[英]Toggling a JQuery animation between two unknown values
I have an image that starts out on the page at a half of its original size. 我在页面上开始显示的图像大小仅为其原始大小的一半。 When the image is clicked, it should expand to this original size.
单击图像后,图像应扩展为原始大小。 When clicked again, it goes back to its half size.
再次单击时,它会返回其一半大小。
I found this JQuery code to toggle the size of an element between two sets of sizes: 我发现此JQuery代码可在两组大小之间切换元素的大小:
$('.expandable').click(function() {
var size = [];
$(this).width() >= 896 ? size = [448, 221] : size = [896, 211];
$(this).stop().animate({width: size[0], height: size[1]}, 'slow');
});
This code works perfectly — as long as you know the original size of the image. 只要您知道图像的原始大小,此代码即可完美运行。 Being an abstraction-minded guy, I set out to try and make this as dynamic as possible.
作为一个具有抽象意识的人,我开始尝试使其尽可能地具有动态性。 I found that, in HTML5, there are naturalWidth and naturalHeight attributes for images.
我发现在HTML5中,图像具有naturalWidth和naturalHeight属性。 So I put this into the code:
所以我将其放入代码中:
$('.expandable').click(function() {
var size = [];
$(this).width() >= $(this).naturalWidth ? size = [$(this).naturalWidth / 2, $(this).naturalHeight / 2] : size = [$(this).naturalWidth, $(this).naturalHeight];
$(this).stop().animate({width: size[0], height: size[1]}, 'slow');
});
Of course, $(this).naturalHeight
and $(this).naturalWidth
both showed that they were undefined when run through a console.log()
(I also tried using them as if they were accessor methods, eg $(this).naturalHeight()
, but the console reported that TypeError: $(...).naturalWidth is not a function
). 当然,
$(this).naturalHeight
和$(this).naturalWidth
都表明在通过console.log()
运行时它们是未定义的(我也尝试将它们当作访问器方法使用,例如$(this).naturalHeight()
,但控制台报告TypeError: $(...).naturalWidth is not a function
)。
I could easily get a list of all the sizes of images on the page, but I am at a loss as to how I would identify which image is which on the page. 我可以轻松地获得页面上所有图像尺寸的列表,但是我对如何识别页面上的图像不知所措。 Maybe as I collect the sizes, I could assign numeric IDs?
也许在收集尺寸时,我可以分配数字ID?
So, the question: How do I toggle the animation between the original size of an image and half of the image's size in an easily expandable way? 那么,问题来了:如何以一种易于扩展的方式在图像的原始尺寸和图像尺寸的一半之间切换动画?
Note: tests were run on the latest version of firefox. 注意:测试是在最新版本的firefox上运行的。
Another note: a lot of answers I've seen say to use the $(...).toggle()
event, but the JQuery API documentation says that this is deprecated , and that we should use the $(...).toggle()
function instead, which toggles the visibility of an element only, and would therefore be inadequate. 另一个注意事项:我已经看到很多答案都说要使用
$(...).toggle()
事件, 但是JQuery API文档说这已经过时了 ,我们应该使用$(...).toggle()
函数代替,它仅切换元素的可见性,因此是不够的。
qtgye's code was almost right, but it caused the image to start at its full size, instead of its half size. qtgye的代码几乎是正确的,但它导致图像以全尺寸而不是一半尺寸开始。 With this in mind, a colleague of mine pointed out that I could just do the same in reverse:
考虑到这一点,我的一位同事指出我可以相反地做同样的事情:
$('.expandable').each(function() {
var w = $(this).width();
var h = $(this).height();
$(this).click(function() {
$(this).animate({
width: ($(this).width() == w ? w * 2 : w),
height: ($(this).height() == h ? h * 2 : h)
});
});
});
Note that instead of dividing the size by two, I now multiply it by two. 请注意,现在不将大小除以2,而是将其乘以2。 Also note that
width
and height
are variables within $(...).animate()
, and not strings. 还要注意,
width
和height
是$(...).animate()
中的变量,而不是字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.