js实现淘宝京东等购物网站中商品图片放大的效果

例子如下:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>图片放大效果</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 350px;
            height: 350px;
            margin: 100px;
            border: 1px solid #ccc;
            position: relative;
        }

        .big {
            width: 400px;
            height: 400px;
            position: absolute;
            top: 0;
            left: 360px;
            border: 1px solid #ccc;
            overflow: hidden;
            display: none;
        }

        .mask {
            width: 175px;
            height: 175px;
            background: rgba(255, 255, 0, 0.4);
            position: absolute;
            top: 0px;
            left: 0px;
            cursor: move;
            display: none;
        }

        .small {
            position: relative;
        }
    </style>
</head>
<body>
<div class="box" id="box">
    <div class="small"><!--小层-->
        <img src="images/small.png" width="350" alt=""/>
        <div class="mask"></div><!--遮挡层-->
    </div><!--小图-->
    <div class="big"><!--大层-->
        <img src="images/big.jpg" width="800" alt=""/><!--大图-->
    </div><!--大图-->
</div>
<!--导入外部的js文件-->
<script>
    //根据id获取元素对象
    function my$(id) {
        return document.getElementById(id);
    }
    //get the elements needed
    var box = my$("box");
    var small = box.children[0];
    var mask = small.children[1];
    var big = box.children[1];
    var bigImg = big.children[0];
    //register mouseover event
    small.onmouseover = function () {
        mask.style.display = "block";
        big.style.display = "block";
    }
    //register mouseleave event
    small.onmouseleave = function () {
        mask.style.display = "none";
        big.style.display = "none";
    }

    //register the mousemove event
    small.onmousemove = function (e) {
        // get the position
        var x = e.clientX;
        var y = e.clientY;
        //solve the problem of margin
        x = x - 100;
        y = y - 100;
        //make the mouse is in the center of mask
        x = x - mask.offsetWidth / 2;
        y = y - mask.offsetHeight / 2;
        //the minimum position of mask
        x = x < 0 ? 0 : x;
        y = y < 0 ? 0 : y;
        //the maximum position of mask
        x = x > small.offsetWidth - mask.offsetWidth ? small.offsetWidth - mask.offsetWidth : x;
        y = y > small.offsetHeight - mask.offsetHeight ? small.offsetHeight - mask.offsetHeight : y;
        //set the position of mask
        mask.style.left = x + "px";
        mask.style.top = y + "px";
        //遮挡层的移动距离/大图的移动距离=遮挡层的最大移动距离/大图的最大移动距离
        //大图的移动距离=遮挡层的移动距离*大图的最大移动距离/遮挡层的最大移动距离
        //大图的横向的最大移动距离
        var maxX = bigImg.offsetWidth-big.offsetWidth;
        //大图的纵向的最大移动距离
        var maxY = bigImg.offsetHeight-big.offsetHeight;
        //大图的横向移动的坐标
        var bigImgMoveX = x*maxX/(small.offsetWidth - mask.offsetWidth);
        //大图的纵向移动的坐标
        var bigImgMoveY = y*maxX/(small.offsetWidth - mask.offsetWidth);
        //设置图片移动
        bigImg.style.marginLeft = -bigImgMoveX+"px";
        bigImg.style.marginTop = -bigImgMoveY+"px";
    }
</script>
</body>
</html>

效果如下:(图片可以自己找)

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现图片的局部放大效果,可以使用JavaScript和CSS来实现。下面是一种实现方式: 1. HTML代码 首先,需要在HTML代码添加图片元素和一个容器元素,用于显示放大后的局部图像。 ```html <img src="image.jpg" id="image" /> <div id="container"></div> ``` 2. CSS样式 在CSS,需要设置容器元素的样式,使其覆盖在图片上方,并且设置该元素的宽度和高度为放大后的局部图像的大小。 ```css #container { position: absolute; top: 0; left: 0; width: 200px; height: 200px; background-repeat: no-repeat; background-size: 400px 400px; display: none; } ``` 3. JavaScript代码 接下来,需要使用JavaScript代码来实现图片局部放大效果。具体实现步骤如下: - 获取图片元素和容器元素。 - 监听图片元素的鼠标移动事件。 - 在事件处理程序,计算当前鼠标位置所对应的局部图像的位置。 - 设置容器元素的背景图像和位置,并显示容器元素。 ```javascript var image = document.getElementById('image'); var container = document.getElementById('container'); image.addEventListener('mousemove', function (e) { var x = e.pageX - this.offsetLeft; var y = e.pageY - this.offsetTop; var bgPosX = -x * 2; var bgPosY = -y * 2; container.style.backgroundImage = 'url(' + this.src + ')'; container.style.backgroundPosition = bgPosX + 'px ' + bgPosY + 'px'; container.style.display = 'block'; }); image.addEventListener('mouseout', function () { container.style.display = 'none'; }); ``` 在上面的代码,我们使用了鼠标移动事件和鼠标移出事件来控制容器元素的显示和隐藏。在鼠标移动事件,我们计算当前鼠标位置所对应的局部图像的位置,并将其设置为容器元素的背景位置。最后,我们显示容器元素。在鼠标移出事件,我们隐藏容器元素。 这样,就可以实现图片的局部放大效果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值