admin管理员组文章数量:1323382
Let's say I have an image, cat.jpg
, and when clicked I want to clone it.
$('img.cat').on("click", function() {
$(this).clone().appendTo('#container');
});
Upon duplication, however, I want the new cat.jpg
to appear as half the size of the original. And I want this to continue happening each time a new cat.jpg
is clicked.
Any ideas on how to go about acplishing this? Is it even possible to inject new styling/classes/parameters via .clone()
?
Let's say I have an image, cat.jpg
, and when clicked I want to clone it.
$('img.cat').on("click", function() {
$(this).clone().appendTo('#container');
});
Upon duplication, however, I want the new cat.jpg
to appear as half the size of the original. And I want this to continue happening each time a new cat.jpg
is clicked.
Any ideas on how to go about acplishing this? Is it even possible to inject new styling/classes/parameters via .clone()
?
- After you clone an element - it's a separated DOM element you can do whatever you want to do with. – zerkms Commented Jan 6, 2014 at 0:07
- Why not head over to jsfiddle and try it out yourself? – Christian Commented Jan 6, 2014 at 0:14
- Been using jsfiddle all day and night. ;-) Just got stumped was all. – daveycroqet Commented Jan 6, 2014 at 0:49
4 Answers
Reset to default 4It sounds like the following is what you're after:
// If all images are within #container, use $("#container") instead:
$(document).on("click", "img.cat", function () {
var original = $(this);
original.clone().css({
width: original.width() / 2,
height: original.height() / 2
}).appendTo("#container");
});
Fiddle: http://jsfiddle/G6XTz/
Of course, you may have wanted the newly added image to be half the size of the last cat image, rather than the cat image clicked:
Fiddle2: http://jsfiddle/G6XTz/1/
Caveat:
The width and height can only divide so far; eventually you'll run into some problems. Better check the result of division first, and make a decision to do something else when it makes sense.
Just setting the width to half seems to be enough with an img element, the height gets set automatically in proportion to the width:
$('#container').on('click','img.cat', function() {
$(this).clone()
.appendTo('#container')
.width(function(i,v) { return v/2;});
});
Demo: http://jsfiddle/Mr2x8/
But if you find you need to set the width and the height here's one way to do it:
$('#container').on('click','img.cat', function() {
var $this = $(this);
$this.clone()
.appendTo('#container')
.width($this.width()/2)
.height($this.height()/2);
});
Demo: http://jsfiddle/Mr2x8/1/
id do this:
$(this).clone().addClass('small').appendTo('#container');
this adds the css class small to the clone of this.
Create a new class with the specific new styling you want to get changed dynamicaly in your CSS file.
.newClass {
//example green outline
outline: solid thin green;
}
And then modify your script:
$('img.cat').on("click", function() {
$(this).clone().addClass('newClass').appendTo('#container');
});
EDIT :
If the only thing you want to change is the size of the img for lets say 10% each click then:
$('img.cat').on("click", function() {
var width = $(this).width() * 0.9;
var height = $(this).height() * 0.9;
$(this).clone().css({"width":width+"px", "height":height+"px"}).appendTo('#container');
});
The above code will produce the same image but 10% smaller than the image clicked .
If you want to click only the initial image then simply put the width
and height
variable outside the click function and update them inside for each click.
NOTE :
In the css()
you add +"px"
if initial width is in px else you add +"%"
if it is in percentage.
本文标签: javascriptjQueryclone() an elementthen add dynamic styling to itStack Overflow
版权声明:本文标题:javascript - jQuery.clone() an element, then add dynamic styling to it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742118220a2421569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论