原生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 相关文章推荐
JQuery 表格操作(交替显示、拖动表格行、选择行等)
Jul 29 Javascript
Javascript基础教程之数组 array
Jan 18 Javascript
JavaScript学习笔记之DOM基础 2.4
Aug 14 Javascript
20分钟轻松创建自己的Bootstrap站点
May 12 Javascript
JS调用Android、Ios原生控件
Jan 06 Javascript
Java中int与integer的区别(基本数据类型与引用数据类型)
Feb 19 Javascript
微信小程序手势操作之单触摸点与多触摸点
Mar 10 Javascript
js中的DOM模拟购物车功能
Mar 22 Javascript
Vue2 模板template的四种写法总结
Feb 23 Javascript
JS实现图片上传多次上传同一张不生效的处理方法
Aug 06 Javascript
解决 viewer.js 动态更新图片导致无法预览的问题
May 14 Javascript
详解vue-router的Import异步加载模块问题的解决方案
May 13 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
PHP开发者常犯的10个MySQL错误更正剖析
2012/01/30 PHP
php smarty truncate UTF8乱码问题解决办法
2014/06/13 PHP
yii2.0之GridView自定义按钮和链接用法
2014/12/15 PHP
基于PHP代码实现中奖概率算法可用于刮刮卡、大转盘等抽奖算法
2015/12/20 PHP
javascript中强制执行toString()具体实现
2013/04/27 Javascript
JavaScript中的比较操作符&gt;、=、
2014/12/31 Javascript
JavaScript实现文本框中默认显示背景图片在获得焦点后消失的方法
2015/07/01 Javascript
关于Vue.js 2.0的Vuex 2.0 你需要更新的知识库
2016/11/30 Javascript
bootstrap模态框垂直居中效果
2016/12/03 Javascript
关于使用js算总价的问题
2017/06/23 Javascript
BootStrap Table前台和后台分页对JSON格式的要求
2017/06/28 Javascript
基于Bootstrap的标签页组件及bootstrap-tab使用说明
2017/07/25 Javascript
JS闭包的几种常见形式实例详解
2017/09/16 Javascript
Vue的实例、生命周期与Vue脚手架(vue-cli)实例详解
2017/12/27 Javascript
Angular使用ControlValueAccessor创建自定义表单控件
2019/03/08 Javascript
jquery实现商品sku多属性选择功能(商品详情页)
2019/12/20 jQuery
Vue父子之间值传递的实例教程
2020/07/02 Javascript
[01:15:45]DOTA2上海特级锦标赛B组小组赛#1 Alliance VS Spirit第一局
2016/02/26 DOTA
[02:49]2018DOTA2亚洲邀请赛主赛事决赛日战况回顾 Mineski鏖战5局夺得辉耀
2018/04/10 DOTA
使用Python读写及压缩和解压缩文件的示例
2016/07/08 Python
Python 多线程实例详解
2017/03/25 Python
Python2随机数列生成器简单实例
2017/09/04 Python
pycharm修改界面主题颜色的方法
2019/01/17 Python
python3.6编写的单元测试示例
2019/08/17 Python
Pycharm小白级简单使用教程
2020/01/08 Python
python中线程和进程有何区别
2020/06/17 Python
用python写爬虫简单吗
2020/07/28 Python
教师应聘个人求职信
2013/12/10 职场文书
本科生职业生涯规划书范文
2014/01/21 职场文书
机关作风建设心得体会
2014/10/22 职场文书
2014年会计主管工作总结
2014/12/20 职场文书
投资意向协议书
2015/01/29 职场文书
工作试用期自我评价
2015/03/10 职场文书
中国现代文学之经典散文三篇
2019/09/18 职场文书
解决MySQL存储时间出现不一致的问题
2021/04/28 MySQL
Java实现房屋出租系统详解
2021/10/05 Java/Android