原生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 相关文章推荐
服务端 VBScript 与 JScript 几个相同特性的写法 By shawl.qiu
Mar 06 Javascript
ExtJS下grid的一些属性说明
Dec 13 Javascript
JS控制文本框textarea输入字数限制的方法
Jun 17 Javascript
当前页禁止复制粘贴截屏代码小集
Jul 24 Javascript
js禁止页面复制功能禁用页面右键菜单示例代码
Aug 29 Javascript
用javascript判断IE版本号简单实用且向后兼容
Sep 11 Javascript
js实现div模拟模态对话框展现URL内容
May 27 Javascript
微信小程序删除处理详解
Aug 16 Javascript
vue登录注册及token验证实现代码
Dec 14 Javascript
详解JS取出两个数组中的不同或相同元素
Mar 20 Javascript
vue.js 2.*项目环境搭建、运行、打包发布的详细步骤
May 01 Javascript
微信小程序关键字变色实现代码实例
Dec 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实现的网络相册图片防盗链完美破解方法
2015/07/01 PHP
PHP编程实现阳历转换为阴历的方法实例
2017/08/08 PHP
JQUERY THICKBOX弹出层插件
2008/08/30 Javascript
百度判断手机终端并自动跳转js代码及使用实例
2014/06/11 Javascript
JQuery遍历DOM节点的方法
2015/06/11 Javascript
JavaScript实现网站访问次数统计代码
2015/08/12 Javascript
jQuery获取复选框被选中数量及判断选择值的方法详解
2016/05/25 Javascript
Ionic如何创建APP项目
2016/06/03 Javascript
jQueryUI DatePicker 添加时分秒
2016/06/04 Javascript
常用原生js自定义函数总结
2016/11/20 Javascript
jQuery EasyUI 获取tabs的实例解析
2016/12/06 Javascript
Bootstrap面板(Panels)的简单实现代码
2017/03/17 Javascript
VUE多层路由嵌套实现代码
2017/05/15 Javascript
JavaScript实现旋转轮播图
2020/08/18 Javascript
jq源码解析之绑在$,jQuery上面的方法(实例讲解)
2017/10/13 jQuery
浅谈JavaScript 代码整洁之道
2018/10/23 Javascript
读懂CommonJS的模块加载
2019/04/19 Javascript
JavaScript中常用的3种弹出提示框(alert、confirm、prompt)
2020/11/10 Javascript
python实现批量图片格式转换
2020/06/16 Python
python实现俄罗斯方块游戏
2020/03/25 Python
Windows下python3.7安装教程
2018/07/31 Python
python爬取微信公众号文章
2018/08/31 Python
wxpython布局的实现方法
2019/11/01 Python
一款简洁的纯css3代码实现的动画导航
2014/10/31 HTML / CSS
英国精品买手店:Browns Fashion
2016/09/29 全球购物
YSL圣罗兰美妆美国官网:Yves Saint Lauret US
2016/11/21 全球购物
eBay比利时购物网站:eBay.be
2019/08/09 全球购物
计算机专业学生求职信分享
2013/12/15 职场文书
大学生演讲稿范文
2014/01/11 职场文书
初中毕业生的自我评价
2014/03/03 职场文书
年度考核自我鉴定
2014/03/19 职场文书
教师作风建设剖析材料
2014/10/11 职场文书
自主招生自荐信范文
2015/03/04 职场文书
五星级酒店前台接待岗位职责
2015/04/02 职场文书
商场营业员岗位职责
2015/04/14 职场文书
PhpSpreadsheet中文文档 | Spreadsheet操作教程实例
2021/04/01 PHP