淘寶之放大鏡
阿新 • • 發佈:2017-09-16
chan cli doc 網頁 需求 height nim ask itl
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.box {
width:350px;
height: 350px;
position: relative;
margin-left: 200px;
margin-top: 200px;
border: 1px solid #000;
}
.smallBox {
position: relative;
}
.mask {
width: 175px;
height: 175px;
background-color: rgba(125, 125, 125, .4);
position: absolute;
left: 0;
top: 0;
display: none;
}
.bigBox {
width: 400px;
height: 400px;
position:absolute;
left: 350px;
top: 0;
overflow: hidden;
display: none;
}
</style>
</head>
<body>
<div class="box">
<div class="smallBox">
<img src="images/001.jpg" />
<div class="mask"></div>
</div>
<div class="bigBox">
<img src="images/0001.jpg" />
</div>
</div>
<script src="toolkit.js"></script>
<script>
//需求1:鼠標進入smallBox區域中,顯示mask 和 bigBox,移除後隱藏。
var box = document.getElementsByTagName("div")[0];
var smallBox = box.children[0];
var bigBox= box.children[1];
var mask = smallBox.children[1];
var bigImg= bigBox.children[0];
smallBox.onmouseover = function(){
show(mask);
show(bigBox);
}
smallBox.onmouseout = function(){
hide(mask);
hide(bigBox);
}
//需求2:mask 跟隨鼠標移動
smallBox.onmousemove = function(event){
//鼠標在網頁中位置
event = event || window.event;
var pageX = event.pageX || event.clientX + scroll().left;
var pageY = event.pageY || event.clientY + scroll().top;
//盒子在網頁中位置
var boxX= box.offsetLeft;
var boxY = box.offsetTop;
//鼠標在盒子中的位置
var sBoxX = pageX - boxX;
var sBoxY = pageY- boxY;
//鼠標在盒子中移動位置有界限
if(sBoxX<0){
sBoxX =0;
}
if(sBoxX>(smallBox.offsetWidth- mask.offsetWidth)){
sBoxX=(smallBox.offsetWidth- mask.offsetWidth)
}
if(sBoxY<0){
sBoxY =0;
}
if(sBoxY>(smallBox.offsetHeight- mask.offsetHeight)){
sBoxY=(smallBox.offsetHeight- mask.offsetHeight)
}
//給mask位置賦值
mask.style.left = sBoxX + "px";
mask.style.top = sBoxY + "px";
//需求3:mask同bigBox等比例移動
//比例公式: smallBox/bigImg = mask/bigBox=sBox/bBox
//bBox = bigBox*sBox/mask;
var bBoxX = bigBox.offsetWidth*sBoxX/mask.offsetWidth;
var bBoxY = bigBox.offsetWidth*sBoxY/mask.offsetHeight;
bigImg.style.marginLeft = -bBoxX + "px";
bigImg.style.marginTop = -bBoxY + "px";
}
</script>
</body>
</html>
引入toolkit.js內容:
function scroll(){
var scroll = {
top: window.pageYOffset || document.documentElement.scrollTop,
left: window.pageXOffset || document.documentElement.scrollLeft
}
return scroll;
}
function animate(ele,target){
clearInterval(ele.timer);
ele.timer = setInterval(function(){
//1.獲取步長
var step = (target -ele.offsetLeft)/10;
//2.二次處理步長
step = step>0? Math.ceil(step):Math.floor(step);
//3、
ele.style.left = ele.offsetLeft+ step +"px";
console.log(1);
if(Math.abs(target -ele.offsetLeft)<=Math.abs(step)){
clearInterval(ele.timer);
ele.style.left = target + "px";
}
},20);
}
function client(){
return {
"width": window.innerWidth || document.documentElement.clientWidth,
"height": window.innerWidth ||document.documentElement.clientWidth,
}
}
function changeColor(){
if(client().width>960){
document.body.style.backgroundColor="blue";
}else if(client().width>640){
document.body.style.backgroundColor="red";
}else{
document.body.style.backgroundColor="orange";
}
}
function getStyle(ele,attr){
if(ele.window.getComputedStyle){
return ele.window.getComputedStyle(ele,null)[attr];
}else{
return ele.currentStyle[attr];
}
}
function show(ele){
ele.style.display="block";
}
function hide(ele){
ele.style.display="none";
}
淘寶之放大鏡