简体   繁体   中英

Jquery dynamic image size change

I have create a logic to auto increment image height and width by using for loop in jquery but my image zoomed suddenlly ,not according to loop .Please help me to resolve my query .

Query is :- image size should be increase in four times according to loop

Thanks all

 $(function() { var plus = 50 ; var max = 4; setTimeout(function(){ for(var i = 0; i < max; i++) { var height = 50; var width = 50; var height = height + plus; var width = width + plus; plus += plus; $("#image").width(width).height(height); } }, 2000); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <body> <div> <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQnk1kzJCdN3FFDcjMIBSNc2YuBdCuc6A5Cpzg4LIDkMB15-mek" id="image"/> </div> </body> 

You would need to use setInterval() and clearInterval() .

Check below example.

 $(function() { var plus = 50; var max = 4; var timer = setInterval(function() { var height = 50; var width = 50; height = height + plus; width = width + plus; plus += plus; $("#image").width(width).height(height); if (plus >= 800) clearInterval(timer); }, 2000); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <body> <div> <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQnk1kzJCdN3FFDcjMIBSNc2YuBdCuc6A5Cpzg4LIDkMB15-mek" id="image" /> </div> </body> 

 $(function() { var plus = 2 ; var max = 4; setInterval(function () { for(var i = 0; i < max; i++) { var height = 2; var width = 2; var height = height + plus; var width = width + plus; plus += plus; $("#image").width(width).height(height); } }, 2000); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <body> <div> <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQnk1kzJCdN3FFDcjMIBSNc2YuBdCuc6A5Cpzg4LIDkMB15-mek" id="image" height="2%" width="2%"/> </div> </body> 

I use setInterval function and set it outside loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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