简单实现js放大镜效果,JS实现电商放大镜效果

2019-06-08 12:41 来源:未知

前端JS电商放大镜效果,供大家参谋,具体内容如下

本文实例为大家享受了js放大镜效果展示的有血有肉代码,供我们参考,具体内容如下

那是我用原生js写的放大镜效果,与丰富多彩的框架技术相比,笔者开心使用原生的js,在此间,想和豪门一起座谈原生和框架技能的明白与民用喜好。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>26-电商放大镜</title>
  <style type="text/css">

  *{
    padding: 0;
    margin: 0;
  }
  #left{
   padding: 0;
  margin: 0;
    width: 400px;
    height: 400px;
    border: 2px solid blue;
    backgroundnull:url(http://chuantu.biz/t6/17/1503469475x2063891122.jpg) no-repeat;
    float: left;
    cursor: crosshair;
    position: relative;
  box-sizing: border-box;
  }
  #box{
    width: 200px;
    height: 200px;
    background: white;
    opacity: 0.6;
    position: absolute;
    top: 0;
    left: 0;
    display: none;
  box-sizing: border-box;
  }
  #cover{
    width: 400px;
    height: 400px;
    background: red;
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
  box-sizing: border-box;
  }
  #right{
    width: 400px;
    height: 400px;
    border: 2px solid black;
    overflow: hidden;
    position: relative;
    display: none;
  box-sizing: border-box;
  }
  #rpic{
    position: absolute;
  }
  </style>

  <script type="text/javascript">

  window.onload = function(){
    var left = document.getElementById("left");
    var right = document.getElementById("right");
    var rpic = document.getElementById("rpic");
    var box = document.getElementById("box");
    var cover = document.getElementById("cover");

    // 给左侧加鼠标移动事件
    cover.onmousemove = function(){

      //获得事件对象
      var ev = window.event;
      var mouse_left = ev.offsetX || ev.layerX;
      var mouse_top = ev.offsetY || ev.layerY;
      // document.title = mouse_left   '|'   mouse_top;

      //计算色块的位置
      var box_left = mouse_left - 100;
      var box_top = mouse_top - 100;

      // 判断是否超出
      if (box_left < 0) {
        box_left = 0;
      }
      if (box_left > 200) {
        box_left = 200;
      }
      if (box_top < 0) {
        box_top = 0;
      }
      if (box_top > 200) {
        box_top = 200;
      }

      // 让色块移动
      box.style.left = box_left   'px';
      box.style.top = box_top   'px';

      //计算右侧图片位置
      var rpic_left = box_left*-2;
      var rpic_top = box_top*-2;

      // 让右侧移动
      rpic.style.left = rpic_left   'px';
      rpic.style.top = rpic_top   'px';

    }

      //给左侧加鼠标移入事件
      cover.onmouseover = function(){
        // 让左侧色块和右侧隐藏
        box.style.display = 'block';
        right.style.display = 'block';
      }

      // 给左侧加鼠标移出事件
      cover.onmouseout = function(){
        // 让左侧色块和右侧隐藏
        box.style.display = 'none';
        right.style.display = 'none';
      }
  }

  </script>
</head>
<body>
  <div id="left">
    <div id="box"></div>
    <div id="cover"></div>
  </div>
  <div id="right">
    <img src="http://chuantu.biz/t6/17/1503469419x2063891122.jpg" id="rpic">
  </div>
</body>
</html>

切实代码如下:

<!DOCTYPE HTML>
<html>
<head>
<title>js放大镜效果</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<style>
*{
margin:0px;
padding:0px;
border:none;
list-style:none;
}
#box{
margin:80px auto;
width: 352px;
}
#box p
{
width: 350px;
height: 350px;
border: 1px solid #ddd;
margin-bottom: 5px; 
}
#box p img
{
width:350px;
height:350px;
}
#box h1
{
width: 352px;
height: 54px;
overflow:hidden; 
position:relative;
}
#box h1 div
{
width:310px;
height: 54px;
margin:0px auto;
position:relative;
}
#box h1 div ul
{
position:absolute;
left: 0px;
top: 0px;
}
#box h1 ul li
{
width: 62px; 
float: left;
}
#box h1 ul li img
{
width: 50px;
height: 50px;
padding: 1px;
border: 1px solid #CECFCE;
}
#box h1 img.hoveredThumb{
border: 2px solid #e4393c;
padding: 0;
}
/*中等大小的图片显示区域*/
p#medImgBox{
position: relative;
}
#jing{
position: absolute;
left: 0;
top: 0;
width: 175px;
height: 175px;
border-radius:50%;
background: #ffd;
opacity: 0.7;
display: none;
}
/*悬于图片/jing上方的专用于接收鼠标移动事件的
一个完全透明的层*/
#medImgBox #mian{
display: block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
cursor: move;
opacity: 0;
}
/**
大图显示区域
**/
#largeImgBox{
position:absolute;
width: 175px;
height: 175px;
border-radius:50%;
border: 1px solid #faa;
top: 0px;
left: 352px; 
overflow: hidden;
display: none;
}
#largeImg{
display: none;
position: absolute;
}
</style> 
</head>
<body >
<div id="box">
<p id="medImgBox">
<img id="mediumImg" src="images/product-s1-m.jpg" />



<img id="largeImg"/>

</p>
<h1>
<div>
<ul id="list">
<li><img src="imagesproduct-s1.jpg" /></li>
<li><img src="imagesproduct-s2.jpg" /></li>
<li><img src="imagesproduct-s3.jpg" /></li>
<li><img src="imagesproduct-s4.jpg" /></li>
<li><img src="imagesproduct-s1.jpg" /></li>
</ul>
</div>
</h1>
</div>
<script>
var imgList = document.querySelectorAll('#box ul li img');
for(var i=0; i<imgList.length; i  ){
var img = imgList[i];
img.onmouseover = changeThumbImg;
}
function changeThumbImg(){
var previousHovered = document.querySelector('.hoveredThumb');
if(previousHovered==null){
this.className = 'hoveredThumb'; 
changeMediumImg(this.src); //修改中等图片的src
}else if(previousHovered!=this){
previousHovered.removeAttribute('class');
this.className = 'hoveredThumb';
changeMediumImg(this.src); //修改中等图片的src
}
}
function changeMediumImg(thumbSrc){
var dotIndex = thumbSrc.lastIndexOf('.');
var prefix = thumbSrc.substring(0, dotIndex);
var suffix = thumbSrc.substring(dotIndex);
var mediumSrc = prefix   '-m' suffix;
document.getElementById('mediumImg').src = mediumSrc;
}
/**
为放大镜jing添加鼠标移动事件
**/
document.querySelector('#mian').onmousemove = 
function(event){
var x = event.offsetX; //事件相对于事件源的偏移量
var y = event.offsetY; 
var jing = document.getElementById('jing'); 
var w = jing.offsetWidth; //jing的宽和高
var h = jing.offsetHeight;
var left = x<w/2 ? 0 : (x-w/2);
var top = y<h/2 ? 0 : (y-h/2);
if(x>(w*2-w/2)){
left = w*2 - w;
}
if(y>(h*2-h/2)){
top = h*2 - h;
}
jing.style.left = left 'px';
jing.style.top = top 'px';
//修改大图的位置/
var largeImg = document.getElementById('largeImg');
largeImg.style.left = (-left*largeImg.width/350)   'px';
largeImg.style.top = (-top*largeImg.height/350) 'px';
}
document.querySelector('#mian').onmouseover = function(){
//显示jing
var jing = document.getElementById('jing');
jing.style.display = 'block';
var largeImgBox = document.getElementById('largeImgBox');
largeImgBox.style.display = 'block';
//获取当前要显示的大图的src
var src = document.querySelector('#mediumImg').src;
var dotIndex = src.lastIndexOf('.');
var prefix = src.substring(0, dotIndex-2);
var suffix = src.substring(dotIndex);
src = prefix   '-l'  suffix;
var largeImg = document.getElementById('largeImg');
largeImg.src = src;
largeImg.style.display='block';
}
document.querySelector('#mian').onmouseout = 
function(){
//除去jing
var jing = document.getElementById('jing');
jing.style.display = 'none';
//除去大图显示区
document.getElementById('largeImgBox').style.display = 'none';
}
</script>
</body>
</html>

以上正是本文的全体内容,希望对大家的读书抱有帮助,也期望我们多多扶助脚本之家。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    body,div,img{
      margin:0;
      padding:0;
    }
    img{
      display:block;
      border:none;
    }
    #box{
      position:absolute;
      top:20px;
      left:20px;
      width:350px;
      height:350px;
      box-shadow:3px 3px 10px 0 #111111;

    }
    #box img{
      width:100%;
      height:100%;

    }
    #mark{
      display:none;
      position:absolute;
      top:0;
      left:0;
      width:175px;
      height:175px;
      background:#000;
      opacity: 0.5;
      filter:alpha(opacity=50);
      cursor:move;

    }
    #boxRight{
      display:none;
      position:absolute;
      top:20px;
      left:380px;
      width:350px;
      height:350px;
      overflow:hidden;
    }
    /*我们右侧的图片的大小是需要严格计算的:
      mark的width是box的width的一半,那么我们的大图宽度也应该是小图的二倍
    */
    #boxRight img{
      position:absolute;
      top:0;
      left:0;
      width:200%;
      height:200%;
    }
  </style>
</head>
<body>
  <div id='box'>
    <img src="img/iphone.jpg" alt="图片 1">
    <div id='mark'></div>
  </div>
  <div id='boxRight'>
    <img src="img/iphone_big.jpg" alt="图片 2">
  </div>
  <script>
    //放大镜的原理: 我们的mark横向是box的一半,纵向也是box的一半,那么右侧的大图横向(纵向)应该是左侧小图的2倍
    var box = document.getElementById('box');
    var mark = document.getElementById('mark');
    var boxRight = document.getElementById('boxRight');
    //设置mark这个盒子
    function setPosition(e){
      var top = e.clientY - box.offsetTop - (mark.offsetHeight/2);
      var left = e.clientX - box.offsetLeft - (mark.offsetWidth/2);
      //边界判断
      var tempL = 0,tempT = 0;
      var minL = 0,minT = 0,maxL = box.offsetWidth - mark.offsetWidth,maxT = box.offsetHeight - mark.offsetHeight ;

      if(left<minL){
        mark.style.left = minL   "px";
        tempL = minL;
      }else if(left>maxL){
        mark.style.left = maxL   "px";
        tempL = maxL;
      }else{
        mark.style.left = left   "px";
        tempL = left;
      }
      if(top<minT){
        mark.style.top = minT   "px";
        tempT = minT;
      }else if(top>maxT){
        mark.style.top = maxT   "px";
        tempT = maxT;
      }else{
        mark.style.top = top   "px";
        tempT = top;
      }
      //右侧图片跟着运动:左侧移动多少,右侧跟着移动他的2倍即可
      var oImg = boxRight.getElementsByTagName("img")[0];
      oImg.style.left = -tempL*2   "px";
      oImg.style.top = -tempT*2   "px";

    }
    box.onmouseenter = function(e){
      e = e || window.event;

      mark.style.display = "block";
      setPosition(e);
      boxRight.style.display = "block";

    }
    box.onmousemove = function(e){
      e = e || window.event;
      setPosition(e);

    }
    box.onmouseleave = function(e){
      e = e || window.event;
      mark.style.display = "none";
      boxRight.style.display = "none";

    }
  </script>
</body>
</html>

如上所述是小编给我们介绍的原生JS完毕的放大镜效果实例代码,希望对我们全部援助,纵然我们有别的难点应接给小编留言,小编会及时回复我们的,在此也非常感激大家对台本之家网站的支撑!

你或者感兴趣的文章:

  • js放大镜放大图片效果
  • JavaScript图片放大镜效果代码[代码比较轻松]
  • 美妙的Javascript图片放大镜
  • 用js达成放大镜的效力的简便实例
  • JavaScript 图片放大镜(可拖放、缩放效果)
  • 原生js写的放大镜效果
  • 电子商务网址上的常用的js放大镜效果
  • JavaScript 图片切割效果(放大镜)
  • JS达成图片放大镜效果的章程
  • 图形放大镜jquery.jqzoom.js使用实例附放大镜Logo

以上正是本文的全体内容,希望对我们的上学抱有帮忙,也冀望大家多多支持脚本之家。

你恐怕感兴趣的稿子:

  • 原生js仿Taobao网商品放大镜效果
  • 原生JS完结轻便放大镜效果
  • Javascript 达成放大镜效果实例详解
  • 基于angularjs达成图片放大镜效果
  • 用js落成放大镜的效果的回顾实例
  • js放大镜放大图片效果
  • JavaScript图片放大镜效果代码[代码比较轻易]
  • 稀奇的Javascript图片放大镜
  • JavaScript 图片放大镜(可拖放、缩放效果)
  • Javascript仿京东放大镜的功能

您或然感兴趣的作品:

  • js放大镜放大图片效果
  • JavaScript图片放大镜效果代码[代码相比轻易]
  • 魔幻的Javascript图片放大镜
  • JavaScript 图片放大镜(可拖放、缩放效果)
  • 用js达成放大镜的成效的简易实例
  • 原生js写的放大镜效果
  • 电子商务网址上的常用的js放大镜效果
  • JavaScript 图片切割效果(放大镜)
  • JS完成图片放大镜效果的艺术
  • 图形放大镜jquery.jqzoom.js使用实例附放大镜Logo
TAG标签: 韦德娱乐1946
版权声明:本文由韦德娱乐1946_韦德娱乐1946网页版|韦德国际1946官网发布于韦德娱乐1946网页版,转载请注明出处:简单实现js放大镜效果,JS实现电商放大镜效果