使用 JQuery 悬停时放大图像的问题
这堆代码似乎确实有效,唯一的问题是过渡不是很顺利。知道如何调整代码以使效果更平滑吗?而且,当图像放大时,图像下方和旁边的所有内容似乎都被推开,以便为放大腾出空间,知道如何阻止这种情况并使图像在放大时与周围的内容重叠吗?另外,不是每次上传新图像并想要这种效果时都为每个图像插入该代码,而是如何使 javascript 代码自动应用于新图像,而不是每次手动将 javascript 应用到它?最后,如何在视频/小部件上使用它而不仅仅是图像? (我使用的是 Dreamweaver CS5)。
$("img").each(function() {
$.data(this, 'size', { width: $(this).width(), height: $(this).height() });
}).hover(function() {
$(this).stop().animate({ height: $.data(this,'size').height*1.2,
width: $.data(this,'size').width*1.2 });
}, function() {
$(this).stop().animate({ height: $.data(this,'size').height,
width: $.data(this,'size').width });
});
The bunch of code does seem to work, only thing is that the transition is not very smooth. Any Idea how to tweak up the code to make the effect more smooth? and also, when the image enlarges, all the content below and beside the image seems to be pushed away to make room for the enlargement, any idea how I can stop this and just make the image overlap the content around it on enlargement? Also, instead of inserting that code for each image every time I upload a new image and want that effect, how do I make the javascript code automatically apply to the new image instead of manually applying the javascript to it each time? Finally, how do I use this on videos/widgets instead of just images? (I’m using Dreamweaver CS5).
$("img").each(function() {
$.data(this, 'size', { width: $(this).width(), height: $(this).height() });
}).hover(function() {
$(this).stop().animate({ height: $.data(this,'size').height*1.2,
width: $.data(this,'size').width*1.2 });
}, function() {
$(this).stop().animate({ height: $.data(this,'size').height,
width: $.data(this,'size').width });
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望图像在调整大小时与任何周围元素重叠,则必须使用 CSS 来相对、绝对或固定定位元素,然后将图像的 z-index 设置为高于周围元素的值。
If you want the image to overlap any surrounding elements upon resize you will have to play with the CSS to position the element either relative, absolute, or fixed, then set the z-index of the image to a value higher than the surrounding elements.
听起来也许你应该研究一下 jQuery Lightbox 插件。至于放大图像推动内容的具体问题,需要将其从文档流中删除(使用位置属性)......但这只是冰山一角。您想要实现的任务有很多移动部分,这就是为什么使用现有的 javascript 插件是最好的 - 当然,IMO。
您可能需要查看 http://www. no-margin-for-errors.com/projects/prettyphoto-jquery-lightbox-clone/ 以及 http://line25.com/articles/rounding-up-the -top-10-jquery-lightbox-scripts 。
Sounds maybe like you should look into a jQuery Lightbox plugin. As for the specific issue with the enlarged image pushing your content around, it needs to be removed from the document flow (using position attribute)... but that's just the tip of an iceberg. There's a lot of moving parts to the task you're trying to achieve, that's why using an existing javascript plugin would be best - IMO, of course.
You might want to check out http://www.no-margin-for-errors.com/projects/prettyphoto-jquery-lightbox-clone/ and also http://line25.com/articles/rounding-up-the-top-10-jquery-lightbox-scripts .