标签:nbsp cti 相同 中比 top val 提示 src 脚本
如果在网上我们发现图片太小,看不清该怎么办?当然是放大啊,放大有很多种方式,这里介绍其中比较简单的一种。
我们可以通过点击按钮放大和缩小图片,每次都会按照相同的倍数,并且不会无限放大和缩小,如果达到了最值,会由相应的提示。先看以下代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>自制缩放</title>
<style>
#wrapper{
width: 500px;
margin: 0 auto;
position: relative;
}
#input{
position: absolute;
top: 0px;
}
input{
color:green;
background: white;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="input">
<input type="button" value="放大" id="enlarge"><input type="button" value="缩小" id="narrow">
</div>
<img src="4.jpg" alt="this is a picture" id="myImage">
</div>
<script>
window.onload=function(){
var image=document.getElementById("myImage");
var largeButton=document.getElementById("enlarge");
largeButton.onclick=function(){
larger();
}
var maxWidth=image.width*2.5;
var maxHeight=image.height*2.5;
function larger(){
var endHeight=image.height*1.3;
var endWidth=image.width*1.3;
var largeTimer=setInterval(function(){
if(image.width<endWidth){
if(image.width<maxWidth){
image.width=image.width*1.03;
image.height=image.height*1.03;
}else{
alert("此图片已经放到了最大。");
clearInterval(largeTimer);
}
}else{
clearInterval(largeTimer);
}
},80);
}
var smallButton=document.getElementById("narrow");
smallButton.onclick=function(){
smaller();
}
var minWidth=image.width*0.5;
var minHeight=image.height*0.5;
function smaller(){
var endWidth=image.width*0.7;
var endHeight=image.height*0.7;
var smallTimer=setInterval(function(){
if(image.width>endWidth){
if(image.width>minWidth){
image.width=image.width*0.95;
image.height=image.height*0.95;
}else{
alert("此图片已经缩到了最小");
clearInterval(smallTimer);
}
}else{
clearInterval(smallTimer);
}
},130);
}
}
</script>
</body>
</html>
这里的JavaScript代码最需要理解的有以下几点:
最终效果如下:
A原始大小图片

B放大图片

C.放大到不能继续

D.缩小图片

E.缩小到不能再小图片

标签:nbsp cti 相同 中比 top val 提示 src 脚本
原文地址:http://www.cnblogs.com/zhuzhenwei918/p/6034512.html