简体   繁体   English

jQuery.clone()一个元素,然后添加动态样式

[英]jQuery.clone() an element, then add dynamic styling to it

Let's say I have an image, cat.jpg , and when clicked I want to clone it. 假设我有一个图像, cat.jpg ,点击后我想克隆它。

$('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. 但是,在复制时,我希望新的cat.jpg显示为原始尺寸的一半。 And I want this to continue happening each time a new cat.jpg is clicked. 我希望每次点击新的cat.jpg都会继续发生这种情况。

Any ideas on how to go about accomplishing this? 关于如何实现这一目标的任何想法? Is it even possible to inject new styling/classes/parameters via .clone() ? 甚至可以通过.clone()注入新的样式/类/参数吗?

It 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.net/G6XTz/ 小提琴: http//jsfiddle.net/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.net/G6XTz/1/ 小提琴2: http//jsfiddle.net/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: 使用img元素将宽度设置为一半似乎就足够了,高度会根据宽度自动设置:

$('#container').on('click','img.cat', function() {
    $(this).clone()
           .appendTo('#container')
           .width(function(i,v) { return v/2;});
});

Demo: http://jsfiddle.net/Mr2x8/ 演示: http//jsfiddle.net/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.net/Mr2x8/1/ 演示: http//jsfiddle.net/Mr2x8/1/

id do this: id这样做:

$(this).clone().addClass('small').appendTo('#container'); 。$(本).clone()addClass( '小')appendTo( '#集装箱');

this adds the css class small to the clone of this. 这会将css类添加到小的克隆中。

Create a new class with the specific new styling you want to get changed dynamicaly in your CSS file. 使用要在CSS文件中动态更改的特定新样式创建一个新类。

.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的大小,那么每次点击可以说10%:

$('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 . 上面的代码将生成相同的图像,但比点击的图像小10%。

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. 如果只想单击初始图像,则只需将widthheight变量放在click函数之外,并在每次单击时更新它们。

NOTE : 注意 :

In the css() you add +"px" if initial width is in px else you add +"%" if it is in percentage. css() ,如果初始宽度为+"px"则添加+"px" ,否则添加+"%"如果它是百分比)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM