js原生之淘宝放大镜特效

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
html,body{
margin:0;
padding:0;
}
#maglifier{
width:1030px;
height:520px;
border:1px solid red;
box-sizing:border-box;
}

.sdiv{
position:absolute;
top:10px;
left:10px;
width:500px;
height:500px;
overflow: hidden;
}
.sdiv img{
position: absolute;
top:0;
left:0;
display: block;
width:100%;
}
.sdiv span{
position: absolute;
top:0;
left:0;
width:250px;
height:250px;
background-color: yellow;
opacity:0.5;
filter: alpha(opacity=50);
display:none;
cursor:crosshair;
}
.bDiv{
position: absolute;
top:10px;
left:520px;
width:500px;
height:500px;
overflow: hidden;
border:1px solid red;
}
.bDiv img{
position: absolute;
top:0;
left:0;
width:200%;
height:200%;
display: block;
}
</style>
<!-- 为了兼容IE8 -->
<!--[if IE 8]>
  <script type="text/javascript">
    if(!document.getElementsByClassName){
      document.getElementsByClassName = function(className, element){
        var children = (element || document).getElementsByTagName(‘*‘);
        var elements = new Array();
        for (var i=0; i<children.length; i++){
          var child = children[i];
          var classNames = child.className.split(‘ ‘);
          for (var j=0; j<classNames.length; j++){
            if (classNames[j] == className){
              elements.push(child);
              break;
            }
          }
        }
        return elements;
      };
    }
  </script>
<![endif]-->
</head>

<body>
<div id="maglifier">
<div class="sdiv">
<img src="image/20170411123451.jpg" alt=‘小图片‘>
<span class="moveSpan"></span>
</div>
</div>

<script type="text/javascript">
var oMaglifier = document.getElementById("maglifier");
var oSDiv = document.getElementsByClassName("sdiv")[0];
var oMoveSpan = document.getElementsByClassName("moveSpan")[0];

// 创建放大的图片元素
var oBDiv = document.createElement("div"),
oBImg = document.createElement("img");
oBDiv.className = "bDiv";
oBImg.src = "image/20170411123430.jpg";
oBImg.alt = "大图片";
oBImg.className = "bImg";

oSDiv.onmouseover = function (){
oBDiv.appendChild(oBImg);
oMoveSpan.style.display = "block";
oMaglifier.appendChild(oBDiv);
};

oSDiv.onmouseout = function (){
oMoveSpan.style.display = "none";
oMaglifier.removeChild(oBDiv);
};

oSDiv.onmousemove = function (e){
var e = e || window.event;

var oMoveSpan_MaxL = oSDiv.offsetWidth - oMoveSpan.offsetWidth; //获取移动框的水平临界值
var oMoveSpan_MaxH = oSDiv.offsetHeight - oMoveSpan.offsetHeight;//获取移动框的垂直临界值

var oBImg_MaxL = -(oBImg.offsetWidth - oBDiv.offsetWidth); //获取移动大图片的临界值
var oBImg_MaxH = -(oBImg.offsetHeight - oBDiv.offsetHeight);//获取移动大图片的临界值

var scrollX = document.documentElement.scrollLeft || document.body.scrollLeft;
var scrollY = document.documentElement.scrollTop || document.body.scrollTop;
var x = e.pageX || e.clientX + scrollX;
var y = e.pageY || e.clientY + scrollY;
//因为ie是没有pageX的,而clientX又表示可视区域,当页面缩小时会有bug,这里重写pageX
//x,y分别表示新的pageX,pageY

var oMoveSpan_L = x - this.offsetLeft - oMoveSpan.offsetWidth/2;
var oMoveSpan_H = y - this.offsetTop - oMoveSpan.offsetHeight/2;
//移动框距离固定框1的left,top值

var oBImg_L = oMoveSpan_L/oMoveSpan_MaxL*oBImg_MaxL;
var oBImg_H = oMoveSpan_H/oMoveSpan_MaxH*oBImg_MaxH;
//大图片距离固定框2的left,top值

if(oMoveSpan_L < 0){oMoveSpan_L = 0;}
if(oMoveSpan_L > oMoveSpan_MaxL){oMoveSpan_L = oMoveSpan_MaxL;}
if(oMoveSpan_H < 0){oMoveSpan_H = 0;}
if(oMoveSpan_H > oMoveSpan_MaxH){oMoveSpan_H = oMoveSpan_MaxH;}
if(oBImg_L > 0){oBImg_L = 0;}
if(oBImg_L < oBImg_MaxL){oBImg_L = oBImg_MaxL;}
if(oBImg_H > 0){oBImg_H = 0;}
if(oBImg_H < oBImg_MaxH){oBImg_H = oBImg_MaxH;}
//到达临界值时,让它不再移动

oMoveSpan.style.left = oMoveSpan_L + "px";
oMoveSpan.style.top = oMoveSpan_H + "px";
oBImg.style.left = oBImg_L + "px";
oBImg.style.top = oBImg_H + "px";

};
</script>

</body>

</html>

时间: 2024-10-05 10:57:59

js原生之淘宝放大镜特效的相关文章

淘宝放大镜效果

淘宝放大镜效果,其实就是一张小图片,还有一张大图片.鼠标移动,按比例显示大图的一部分,在大图的父元素上设置overflow:hidden,就OK了.这里面需要注意换算好大小图的比例. 代码地址:https://github.com/peng666/blogs/tree/gh-pages/floats 在线测试地址:http://peng666.github.io/blogs/floats/

原生JS实现简单的淘宝放大镜效果

大家经常去淘宝买东西会发现,淘宝上的放大镜效果挺有意思的,这里简单的实现了下,代码中的图片地址 亲们可以自行更换; <!DOCTYPE html> <html> <head> <title>放大镜</title> <meta charset="utf-8" /> <style type="text/css"> *{ margin: 0; padding: 0; } body{ hei

如何使用纯JS过掉淘宝滑块

起因 众所周知淘宝滑块很难过掉,今天博主就专门研究了一下淘宝滑块,之前博主也有研究过但是发现并不好过.今天恰好有个项目需要淘宝登录,就有滑块验证,说明一下博主做的是浏览器插件哦.今天博主打算在研究滑块.记录一下博主的解密过程. 排查 首先咱们打开淘宝登录页面,这里我就以阿里妈妈的超级推荐登录为例登录地址如下:登录地址打开之后我们来到输入账户密码的地方由于要实现自动登录的功能所以需要跟编辑框赋值,但是淘宝是使用react写的所以一般情况下是无法外部赋值的在React和Vue如何使用原生JS赋值中我

原生js淘宝放大镜效果(第二版)

随便写写 <!DOCTYPE html> <html> <head> <title>放大镜</title> <meta charset="utf-8" /> <style type="text/css"> *{ margin: 0; padding: 0; } body{ height: 2000px; } #wrapleft{ width: 300px; height: 400px

js 实现淘宝放大镜

<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title></title> <style> *{ margin: 0; padding: 0; } .div1, .div2 { width: 430px; height: 430px; float: left; cursor:crosshair; } .di

JQ实战天猫淘宝放大镜

这个特效平时生活中很常见,话不多说先上效果图. 首先布局,遮罩层是这个效果中的重点精华,也就是下面代码中的shade. <style> * { margin:0px; padding:0px;} #box { width:400px; height:400px; border:5px solid #CCC; margin:100px auto; position:relative;} #box .simg {width:400px; height:400px; position:relativ

js 实现仿 淘宝 五星评价 demo

<style> @font-face { font-family: 'iconfont'; /* project id 247957 */ src: url('//at.alicdn.com/t/font_wkv6intmx8cnxw29.eot'); src: url('//at.alicdn.com/t/font_wkv6intmx8cnxw29.eot?#iefix') format('embedded-opentype'), url('//at.alicdn.com/t/font_wk

淘宝放大镜

HTML 1 <img src="img/eddbc6f71def8b33d207222b32a6d1f9.jpg"> 2 <canvas id="can" width="200px" height="200px"></canvas> 3 <div id="div"></div> CSS 1 <style type="text/cs

js运动 模仿淘宝幻灯

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> *{ margin:0; padding: 0; } #div1{ width:600px;height:400px;border:1px solid #000;margin: 100px auto 0; position: relative