原生js实现图片放大缩小计时器效果


Posted in Javascript onJanuary 20, 2017

知识要点

var fn=setInterval(function(){},1000)

每隔1秒执行一次函数

clearInterval(fn)

清除计时器

判断当图片放大缩小到固定大小时,清除计时器

完整代码

<!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("图片已经是最大值了")
  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("图片已经是最小值了")
  clearInterval(minTime);
  }
 }else{
  clearInterval(minTime);
 }

 },20);
 }
}
window.onload=function(){
 pic_max();
}
</script> 
</body> 
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持三水点靠木!

Javascript 相关文章推荐
Prototype Selector对象学习
Jul 23 Javascript
前端必备神器 Snap.svg 弹动效果
Nov 10 Javascript
JS的数组迭代方法
Feb 05 Javascript
Backbone.js框架中简单的View视图编写学习笔记
Feb 14 Javascript
jquery div模态窗口的简单实例
May 28 Javascript
jquery实现点击页面回到顶部
Nov 23 Javascript
EditPlus中的正则表达式 实战(2)
Dec 15 Javascript
bootstrapValidator.min.js表单验证插件
Feb 09 Javascript
jQuery为DOM动态追加事件的方法
Feb 16 Javascript
javascript实现滑动解锁功能
Mar 22 Javascript
vue+element创建动态的form表单及动态生成表格的行和列
May 20 Javascript
socket在egg中的使用实例代码详解
May 30 Javascript
详解基于angular路由的requireJs按需加载js
Jan 20 #Javascript
原生js实现新闻列表展开/收起全文功能
Jan 20 #Javascript
Vue.js实现表格动态增加删除的方法(附源码下载)
Jan 20 #Javascript
node.js与C语言 实现遍历文件夹下最大的文件,并输出路径,大小
Jan 20 #Javascript
微信小程序通过api接口将json数据展现到小程序示例
Jan 20 #Javascript
BootStrap栅格系统、表单样式与按钮样式源码解析
Jan 20 #Javascript
Vue开发过程中遇到的疑惑知识点总结
Jan 20 #Javascript
You might like
全国FM电台频率大全 - 25 云南省
2020/03/11 无线电
文章推荐系统(三)
2006/10/09 PHP
用PHP制作静态网站的模板框架(一)
2006/10/09 PHP
在PHP中使用X-SendFile头让文件下载更快
2014/06/01 PHP
Codeigniter实现处理用户登录验证后的URL跳转
2014/06/12 PHP
分享PHP函数实现数字与文字分页代码
2015/07/28 PHP
PHP中抽象类、接口的区别与选择分析
2016/03/29 PHP
PHP基于新浪IP库获取IP详细地址的方法
2017/05/04 PHP
PHP实现基于回溯法求解迷宫问题的方法详解
2017/08/17 PHP
PHP分享图片的生成方法
2018/04/25 PHP
json格式化/压缩工具 Chrome插件扩展版
2010/05/25 Javascript
Jquery网页内滑动缓冲导航的实现代码
2015/04/05 Javascript
javascript实现tab切换的四种方法
2015/11/05 Javascript
JavaScript兼容浏览器FF/IE技巧
2016/08/14 Javascript
three.js实现围绕某物体旋转
2017/01/25 Javascript
JS判断一个数是否是水仙花数
2017/06/11 Javascript
vue.js移动端app实战1:初始配置详解
2017/07/24 Javascript
改变vue请求过来的数据中的某一项值的方法(详解)
2018/03/08 Javascript
json数据传到前台并解析展示成列表的方法
2018/08/06 Javascript
python正则表达式修复网站文章字体不统一的解决方法
2013/02/21 Python
python定时采集摄像头图像上传ftp服务器功能实现
2013/12/23 Python
python调用Matplotlib绘制分布点并且添加标签
2018/05/31 Python
pyqt5 使用cv2 显示图片,摄像头的实例
2019/06/27 Python
Python 共享变量加锁、释放详解
2019/08/28 Python
使用Python和百度语音识别生成视频字幕的实现
2020/04/09 Python
python进行参数传递的方法
2020/05/12 Python
python用opencv完成图像分割并进行目标物的提取
2020/05/25 Python
python反编译教程之2048小游戏实例
2021/03/03 Python
AJAX的优缺点都有什么
2015/08/18 面试题
暑期实践思想汇报
2014/01/06 职场文书
师德师风事迹材料
2014/12/20 职场文书
2015年党风建设工作总结
2015/04/29 职场文书
优秀共产党员主要事迹材料
2015/11/05 职场文书
微信小程序实现拍照和相册选取图片
2021/05/09 Javascript
一次SQL如何查重及去重的实战记录
2022/03/13 MySQL
element tree树形组件回显数据问题解决
2022/08/14 Javascript