Zoom an image every one second jQuery

老子叫甜甜 提交于 2019-12-11 00:19:49

问题


I try to animate an image with zooming every one second, it work when I load my page but I want it every one second. I think when I use the same id of my image it's a problem.

This is my JSFIDDLE.

Thank you.


回答1:


You're using setTimeout which will execute some code after a certain time, what you want is setInterval, which will execute some code every so often. http://jsfiddle.net/UxTdU/3/




回答2:


You could just increment the zoom property from within an interval.

setInterval(function(){
    $("#myImg").animate({"zoom":"+=.025"}, 500);
}, 1000);

Fiddle: http://jsfiddle.net/UxTdU/6/

You could even zoom it up to a particular size, then stop:

// Cache reference to image
var $image  = jQuery("#myImg");

// Cache reference to zooming interval
var zooming = setInterval(function(){

    // If current zoom is under 1.5
    $image.css("zoom") < 1.5 

        // Increment zoom by .025
        ? $image.animate({"zoom":"+=.025"}, 500) 

        // Otherwise, stop zooming
        : clearInterval(zooming);

// Run every 1 second
}, 1000);

Fiddle: http://jsfiddle.net/UxTdU/8/




回答3:


http://jsfiddle.net/UxTdU/5/ - use setInterval, not setTimeout. setTimeout happens only once.



来源:https://stackoverflow.com/questions/12937260/zoom-an-image-every-one-second-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!