为了账号安全,请及时绑定邮箱和手机立即绑定

JaveScript 之 浏览商品

模仿淘宝网网页中商品浏览模式,
即当鼠标放在图片上时,旁边会出现当前区域放大的图片
代码如下:(下面有福利哦)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #small{
                width: 478px;
                height: 665px;
                position: relative;
            }
            #small>img{
                width: 100%;
                height: 100%;
                border:5px solid black;
                /*display: block;*/
            }
            #area{
                width: 80px;
                height: 110px;
                background: rgba(80,100,120,0.6);
                position: absolute;
                top: 0;
                left: 0;
                display: none;
            }
            #smaller{
                width: 470px;
                height: 110px;
                margin-top: 20px;
                margin-left: 12px;
            }
            #smaller>img{
                width: 70px;
                height: 98px;
                margin: 0 6px;
                border:2px solid white;
            }
            #big{
                width: 478px;
                height: 665px;
                overflow: hidden;
                position: absolute;
                top: 8px;
                left: 500px;
                display: none;
            }
            #big>img{
                width: 2856px;
                height: 3973px;
                position: absolute;
            }
        </style>
    </head>
    <body>
        <div id="wrap">
            <!-- 左侧图片 -->
            <div id="small">
                <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="../img/0.jpg" alt="" />
                <!-- 选中区域 -->
                <div id="area"></div>
            </div>
            <!-- 商品种类 -->
            <div id="smaller">
                <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="../img/0.jpg" alt="" />
                <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="../img/1.jpg" alt="" />
                <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="../img/2.jpg" alt="" />
                <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="../img/3.jpg" alt="" />
                <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="../img/4.jpg" alt="" />
            </div>
            <!-- 放大的图片 -->
            <div id="big">
                <img class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="../img/0.jpg" alt="" />
            </div>
        </div>
    </body>
    <script type="text/javascript">
        var small = document.getElementById('small');
        var smallImg = document.querySelector('#small>img');
        var areaDiv = document.getElementById('area');
        var smaller = document.getElementById('smaller');
        var imgs = smaller.getElementsByTagName('img');
        var big = document.getElementById('big');
        var bigImg = document.querySelector('#big>img');
        for (var i = 0;i < imgs.length;i++) {
            imgs[i].index = i;
            //当鼠标放在商品上时,上方即出现其放大的图片
            imgs[i].onmouseenter = function () {
                this.style.border = '2px solid black';
                smallImg.src = '../img/' + this.index + '.jpg';
                bigImg.src =  '../img/' + this.index + '.jpg';
            }
            imgs[i].onmouseleave = function () {
                this.style.border = '2px solid white';
                big.removeChild(img);
            }
        }
        //给左侧图片添加鼠标移动时间
        small.onmousemove = function () {
            //定义选中区域页面左侧和上侧的距离,即定义选中区域的位置
            var xDistance = event.clientX - smallImg.offsetLeft - areaDiv.offsetWidth / 2;
            var yDistance = event.clientY - smallImg.offsetTop - areaDiv.offsetHeight / 2;
            //定义选中区域可移动的最大距离
            var maxX = smallImg.offsetWidth - areaDiv.offsetWidth;
            var maxY = smallImg.offsetHeight - areaDiv.offsetHeight;
            //左侧超出
            if (xDistance < 0) {
                xDistance = 0;
            }
            //右侧超出
            if (xDistance > maxX) {
                xDistance = maxX;
            }
            //上侧超出
            if (yDistance < 0) {
                yDistance = 0;
            }
            //下册超出
            if (yDistance > maxY) {
                yDistance = maxY;
            }
            //修改选中区域的位置
            areaDiv.style.left = xDistance + 'px';
            areaDiv.style.top = yDistance + 'px';
            //修改右侧大图的位置
            bigImg.style.left = - xDistance * bigImg.offsetWidth / smallImg.offsetWidth + 'px';
            bigImg.style.top = - yDistance * bigImg.offsetHeight / smallImg.offsetHeight + 'px';
        }
        //为左侧图片添加鼠标移入及移出事件
        small.onmouseenter = function() {
            areaDiv.style.display = 'block';
            big.style.display = 'block';
        }
        small.onmouseleave = function () {
            areaDiv.style.display = 'none';
            big.style.display = 'none';
        }
    </script>
</html>

图片描述

点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消