原生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对象数据缓存Cache原理及jQuery.data方法区别介绍
Apr 07 Javascript
jQuery实现的超酷苹果风格图标滑出菜单效果代码
Sep 16 Javascript
JavaScript性能优化总结之加载与执行
Aug 11 Javascript
完美实现js拖拽效果 return false用法详解
Jul 28 Javascript
Bootstrap 模态框(Modal)带参数传值实例
Aug 20 Javascript
Vue2.0基于vue-cli+webpack父子组件通信(实例讲解)
Sep 14 Javascript
详解Web使用webpack构建前端项目
Sep 23 Javascript
Vue2.0 slot分发内容与props验证的方法
Dec 12 Javascript
vue.js指令v-for使用以及下标索引的获取
Jan 31 Javascript
微信小程序 腾讯地图显示偏差问题解决
Jul 27 Javascript
vue cli3 调用百度翻译API翻译页面的实现示例
Sep 13 Javascript
基于VUE实现简单的学生信息管理系统
Jan 13 Vue.js
详解基于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 Web开发MVC框架的Smarty使用说明
2013/04/19 PHP
php判断手机访问还是电脑访问示例分享
2014/01/20 PHP
PHP、Nginx、Apache中禁止网页被iframe引用的方法
2020/10/01 PHP
js程序中美元符号$是什么
2008/06/05 Javascript
javascript document.compatMode兼容性
2010/02/23 Javascript
jQuery插件开发全解析
2012/10/10 Javascript
JavaScript高级程序设计(第3版)学习笔记6 初识js对象
2012/10/11 Javascript
JavaScript实现x秒后自动跳转到一个页面
2013/01/03 Javascript
js 对小数加法精度处理示例说明
2013/12/27 Javascript
js实现的黑背景灰色二级导航菜单效果代码
2015/08/24 Javascript
Javascript实现Array和String互转换的方法
2015/12/21 Javascript
JS模仿手机端九宫格登录功能实现代码
2016/04/28 Javascript
javascript 中的console.log和弹出窗口alert
2016/08/30 Javascript
js绘制购物车抛物线动画
2020/11/18 Javascript
vue中如何实现变量和字符串拼接
2017/06/19 Javascript
浅谈angular2路由预加载策略
2017/10/04 Javascript
深入理解JavaScript 箭头函数
2019/05/30 Javascript
微信小程序实现pdf、word等格式文件上传的方法
2019/09/10 Javascript
vue+elementui 对话框取消 表单验证重置示例
2019/10/29 Javascript
浅析Python中的多重继承
2015/04/28 Python
Python最火、R极具潜力 2017机器学习调查报告
2017/12/11 Python
修改python plot折线图的坐标轴刻度方法
2018/12/13 Python
Django rest framework jwt的使用方法详解
2019/08/08 Python
keras分类模型中的输入数据与标签的维度实例
2020/07/03 Python
django使用graphql的实例
2020/09/02 Python
在Ubuntu中安装并配置Pycharm教程的实现方法
2021/01/06 Python
同步和异步有何异同,在什么情况下分别使用他们?举例说明
2014/02/27 面试题
三好学生自我鉴定
2013/12/17 职场文书
绿色学校实施方案
2014/03/31 职场文书
竞选劳动委员演讲稿
2014/04/28 职场文书
事业单位考核材料
2014/05/21 职场文书
违反交通法规检讨书
2014/09/10 职场文书
网站出售协议书范文
2014/10/10 职场文书
2014年卫生保健工作总结
2014/12/08 职场文书
《刷子李》教学反思
2016/02/20 职场文书
自制短波长线天线频率预选器 - 成功消除B2K之流的镜像
2021/04/22 无线电