-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.wrap { | ||
margin: 20px auto; | ||
width: 500px; | ||
img { | ||
display: block; | ||
margin: 0 auto; | ||
width: 300px; | ||
height: auto; | ||
} | ||
.controls { | ||
margin-top: 20px; | ||
text-align: center; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head lang="en"> | ||
<meta charset="UTF-8"> | ||
<title>图片缓慢缩放效果</title> | ||
<link rel="stylesheet" href="css/demo.css"/> | ||
</head> | ||
<body> | ||
<div class="wrap"> | ||
<img src="images/mm.jpeg" alt=""/> | ||
|
||
<div class="controls"> | ||
<button id="zoomOutBtn">放大</button> | ||
<button id="zoomInBtn">缩小</button> | ||
</div> | ||
</div> | ||
<script src="js/demo.js"></script> | ||
</body> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Created by gaopengfei on 2016/5/3. | ||
*/ | ||
|
||
var img = document.querySelector('img'); | ||
|
||
var timer; | ||
|
||
// getComputedStyle返回的CSS属性带单位的 | ||
const REAL_IMAGE_WIDTH = img.offsetWidth; | ||
const MAX_WIDTH = REAL_IMAGE_WIDTH * 2, | ||
MIN_WIDTH = REAL_IMAGE_WIDTH * .5; | ||
|
||
const INTERVAL = 20; // 控制缩放的速度 | ||
|
||
var zoomIn = function () { | ||
var endWidth = img.offsetWidth * .7; | ||
if (img.offsetWidth < MIN_WIDTH) { | ||
return alert('图片缩小到最小值'); | ||
} | ||
clearInterval(timer); | ||
timer = setInterval(function () { | ||
if (img.width > endWidth) { | ||
img.style.width = img.offsetWidth * .95 + 'px'; | ||
} else { | ||
clearInterval(timer); | ||
} | ||
}, INTERVAL); | ||
}; | ||
|
||
var zoomOut = function () { | ||
var endWidth = img.offsetWidth * 1.3; | ||
if (img.width > MAX_WIDTH) { | ||
return alert('图片放大到最大值'); | ||
} | ||
clearInterval(timer); | ||
timer = setInterval(function () { | ||
|
||
if (img.offsetWidth < endWidth) { | ||
img.style.width = img.offsetWidth * 1.05 + 'px'; | ||
} else { | ||
clearInterval(timer); | ||
} | ||
}, INTERVAL); | ||
}; | ||
|
||
document.querySelector('#zoomInBtn').addEventListener('click', function () { | ||
zoomIn(); | ||
}); | ||
document.querySelector('#zoomOutBtn').addEventListener('click', function () { | ||
zoomOut(); | ||
}); |