原生js實(shí)現(xiàn)圖片放大縮小計(jì)時(shí)器效果
知識(shí)要點(diǎn)
var fn=setInterval(function(){},1000)
每隔1秒執(zhí)行一次函數(shù)
clearInterval(fn)
清除計(jì)時(shí)器
判斷當(dāng)圖片放大縮小到固定大小時(shí),清除計(jì)時(shí)器
完整代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>demo</title> <style> body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0;} body,button,input,select,textarea{font:12px/1.5 tahoma,arial,\5b8b\4f53;} h1,h2,h3,h4,h5,h6{font-size:100%;} address,cite,dfn,em,var{font-style:normal;} code,kbd,pre,samp{font-family:courier new,courier,monospace;} small{font-size:12px;} ul,ol{list-style:none;} a{text-decoration:none;} a:hover{text-decoration:underline;} sup{vertical-align:text-top;} sub{vertical-align:text-bottom;} legend{color:#000;} fieldset,img{border:0;} button,input,select,textarea{font-size:100%;} table{border-collapse:collapse;border-spacing:0;} .clear{clear: both;float: none;height: 0;overflow: hidden;} </style> </head> <body> <div style="width:400px;margin:0 auto;"> <img src="http://img.mukewang.com/53577ee900016c2102080260.jpg" id="myImage" /><br> <input type="button" id="max" value="放大" /> <input type="button" id="min" value="縮小" /> </div> <script type="text/javascript"> function pic_max(){ var maxBtn=document.getElementById("max"); var minBtn=document.getElementById("min"); maxBtn.onclick=function(){ max(); } var img=document.getElementById("myImage"); var maxHeight=img.height*2; var maxWidth=img.width*2; function max(){ var endHeight=img.height*1.3; var endWidth=img.width*1.3; var maxTime=setInterval(function(){ if(img.height<endHeight&&img.width<endWidth){ if(img.height<maxHeight&&img.width<maxWidth){ img.height=img.height*1.05; img.width=img.width*1.05; }else{ alert("圖片已經(jīng)是最大值了") clearInterval(maxTime); } }else{ clearInterval(maxTime); } },20); } minBtn.onclick=function(){ min(); } var img=document.getElementById("myImage"); var minHeight=img.height*0.5; var minWidth=img.width*0.5; function min(){ var overHeight=img.height*0.7; var overWidth=img.width*0.7; var minTime=setInterval(function(){ if(img.height>overHeight&&img.width>overWidth){ if(img.height>minHeight&&img.width>minWidth){ img.height=img.height*0.95; img.width=img.width*0.95; }else{ alert("圖片已經(jīng)是最小值了") clearInterval(minTime); } }else{ clearInterval(minTime); } },20); } } window.onload=function(){ pic_max(); } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
- JS簡單的圖片放大縮小的兩種方法
- JS網(wǎng)頁圖片查看器(兼容IE、FF)可控制圖片放大縮小移動(dòng)
- javascript 圖片放大縮小功能實(shí)現(xiàn)代碼
- JS實(shí)現(xiàn)圖片放大縮小的方法
- js實(shí)現(xiàn)用滾動(dòng)條來放大縮小圖片的代碼
- js實(shí)現(xiàn)圖片旋轉(zhuǎn) js滾動(dòng)鼠標(biāo)中間對(duì)圖片放大縮小
- js實(shí)現(xiàn)圖片緩慢放大縮小效果
- js實(shí)現(xiàn)圖片放大縮小功能后進(jìn)行復(fù)雜排序的方法
- js實(shí)現(xiàn)使用鼠標(biāo)拖拽切換圖片的方法
- 原生JS實(shí)現(xiàn)拖拽圖片效果
- JS實(shí)現(xiàn)放大、縮小及拖拽圖片的方法【可兼容IE、火狐】
- JavaScript實(shí)現(xiàn)圖片的放大縮小及拖拽功能示例
相關(guān)文章
dedecms頁面如何獲取會(huì)員狀態(tài)的實(shí)例代碼
下面小編就為大家?guī)硪黄猟edecms頁面如何獲取會(huì)員狀態(tài)的實(shí)例代碼。一起跟隨小編過來看看吧,希望對(duì)大家有所幫助。2016-03-03BootStrap+Mybatis框架下實(shí)現(xiàn)表單提交數(shù)據(jù)重復(fù)驗(yàn)證
這篇文章主要介紹了BootStrap+Mybatis框架下實(shí)現(xiàn)表單提交數(shù)據(jù)重復(fù)驗(yàn)證功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03js從Cookies里面取值的簡單實(shí)現(xiàn)
遇到一個(gè)Js從Cookies里面取值的需求,Js貌似沒有現(xiàn)成的方法可以指定Key值獲取Cookie里面對(duì)應(yīng)的值,簡單實(shí)現(xiàn)如下2014-06-06js實(shí)現(xiàn)按鈕顏色漸變動(dòng)畫效果
這篇文章主要介紹了js實(shí)現(xiàn)按鈕顏色漸變動(dòng)畫效果的方法,涉及javascript鼠標(biāo)事件及頁面表單元素樣式的動(dòng)態(tài)操作技巧,需要的朋友可以參考下2015-08-08Typescript 中的 interface 和 type 到底有什么區(qū)別詳解
這篇文章主要介紹了Typescript 中的 interface 和 type 到底有什么區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06