原生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 相关文章推荐
用ADODB.Stream转换
Jan 22 Javascript
开发跨浏览器javascript常见注意事项
Jan 01 Javascript
jquery.messager.js插件导致页面抖动的解决方法
Jul 14 Javascript
解决extjs grid 不随窗口大小自适应的改变问题
Jan 26 Javascript
调用innerHTML之后onclick失效问题的解决方法
Jan 28 Javascript
Javascript学习笔记之数组的遍历和 length 属性
Nov 23 Javascript
基于jquery实现二级联动效果
Mar 30 jQuery
vue+axios 前端实现登录拦截的两种方式(路由拦截、http拦截)
Oct 24 Javascript
详解vue路由篇(动态路由、路由嵌套)
Jan 27 Javascript
js中的深浅拷贝问题简析
May 10 Javascript
Vue动态面包屑功能的实现方法
Jul 01 Javascript
Vue点击切换Class变化,实现Active当前样式操作
Jul 17 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
PHP7+Nginx的配置与安装教程详解
2016/05/10 PHP
php数组函数array_walk用法示例
2016/05/26 PHP
PHP 中 var_export、print_r、var_dump 调试中的区别
2018/06/19 PHP
php实现session共享的实例方法
2019/09/19 PHP
JavaScript事件处理器中的event参数使用介绍
2013/05/24 Javascript
Javascript call和apply区别及使用方法
2013/11/14 Javascript
angularjs实现与服务器交互分享
2014/06/24 Javascript
《JavaScript DOM 编程艺术》读书笔记之JavaScript 语法
2015/01/09 Javascript
jQuery向后台传入json格式数据的方法
2015/02/13 Javascript
JavaScript获得指定对象大小的方法
2015/07/01 Javascript
JavaScript实现仿新浪微博大厅和腾讯微博首页滚动特效源码
2015/09/15 Javascript
JavaScript如何实现在文本框(密码框)输入提示语
2015/12/25 Javascript
js中scrollTop()方法和scroll()方法用法示例
2016/10/03 Javascript
微信小程序 生命周期详解
2016/10/12 Javascript
详解vue-Resource(与后端数据交互)
2017/01/16 Javascript
angular.js4使用 RxJS 处理多个 Http 请求
2017/09/23 Javascript
webpack学习教程之前端性能优化总结
2017/12/05 Javascript
vue  自定义组件实现通讯录功能
2018/09/30 Javascript
webpack4实现不同的导出类型
2019/04/09 Javascript
从0到1搭建element后台框架优化篇(打包优化)
2019/05/12 Javascript
vue给对象动态添加属性和值的实例
2019/09/09 Javascript
详解小程序云开发攻略(解决最棘手的问题)
2019/09/30 Javascript
让你30分钟快速掌握vue3教程
2020/10/26 Javascript
python实现的简单抽奖系统实例
2015/05/22 Python
Python中的time模块与datetime模块用法总结
2016/06/30 Python
一份python入门应该看的学习资料
2018/04/11 Python
python统计字母、空格、数字等字符个数的实例
2018/06/29 Python
在PyCharm中批量查找及替换的方法
2019/01/20 Python
使用python创建Excel工作簿及工作表过程图解
2020/05/27 Python
伦敦一家非常流行的时尚精品店:Oxygen Boutique
2017/01/15 全球购物
营销与策划个人求职信
2013/09/22 职场文书
物业管理应届生求职信
2013/10/28 职场文书
企业门卫岗位职责
2013/12/12 职场文书
大二自我鉴定
2014/01/31 职场文书
农民工工资承诺书范文
2014/03/31 职场文书
班主任个人工作反思
2014/04/28 职场文书