• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
暖文
博客园    首页    新随笔    联系   管理    订阅  订阅

模拟淘宝放大镜小例子

 body {
                background-color: #D8E7FA;
            }
            /*取消默认样式*/
            ul {
                margin: 0px;
                padding: 0px;
                list-style: none;
            }
            /*大框样式*/
            .itmearea {
                position: relative;
                width: 400px;
                height: 480px;
                border: 1px solid #888;
                margin: 100px auto;
            }
            /*下面的距离*/
            .itmearea .pic {
                margin-bottom: 15px;
            }
            /*大框里面的小框*/
            .itmearea .pic .cover {
                display: none;
                position: absolute;
                cursor: all-scroll;
                left: 0;
                top: 0;
                width: 200px;
                height: 200px;
                opacity: .2;
                z-index: 3;
                background: 50% top no-repeat #000;
            }
            /*图片大小*/
            .itmearea .pic img {
                width: 400px;
                height: 400px;
            }
            /*大框下面的小图片框*/
            .itmearea .list {
                display: flex;
            }
            /*每一个小图*/
            .itmearea .list li {
                margin: auto;
            }
            /*小框里的图片*/
            .itmearea .list img {
                display: block;
                width: 50px;
                height: 50px;
            }
            /*隐藏的放大镜*/
            .itmearea .deteil {
                position: absolute;
                display: none;
                top: -1px;
                left: 400px;
                width: 500px; 
                height: 479px;
                border: 1px solid #888;
                background-image: url(img/1.jpg);
                background-size: 150%;
            }
            /*大框下移入小框的边框*/
            .itmearea .list .current {
                border: 2px red solid;
            }
<div class="itmearea">
        <div class="pic">    
            <img src="img/1.jpg" />
            <div class="cover"></div>
        </div>
        <ul class="list">
            <li><img src="img/1.jpg" class="current" /></li>
            <li><img src="img/2.jpg" /></li>
            <li><img src="img/3.jpg" /></li>
            <li><img src="img/4.jpg" /></li>
            <li><img src="img/5.jpg" /></li>
        </ul>
        <div class="deteil"></div>
    </div>
 var list = document.querySelector('.list'),
                imgs = list.querySelectorAll('img'),
                
                img = document.querySelector('.pic img'),
                pic = document.querySelector('.itmearea .pic'),
                cover = document.querySelector('.cover'),
                deteil = document.querySelector('.deteil');
    //鼠标移入监听事件
            list.addEventListener('mousemove', function(e) {
                //根据标签判断  , 移入哪个img图片
                if(e.target.tagName == 'IMG') {
                    img.src = e.target.src;
                    //显示哪一张图片
                    deteil.style.backgroundImage = 'url(' + e.target.src + ')';
                    
                    // 遍历图片  清空样式
                    imgs.forEach(function(item) {
                        item.className = '';
                    })
                    //给当前图片添加一个class类  两像素的边框
                    e.target.className = 'current';

                }
            })
            //给大框绑定鼠标移入事件
            pic.addEventListener('mousemove', function(e) {
                var x = e.clientX,
                    y = e.clientY,
                    cx = pic.getBoundingClientRect().left,
                    cy = pic.getBoundingClientRect().top,
                    tx = x - cx - 100,
                    ty = y - cy - 100;
                if(tx < 0) {
                    tx = 0;
                }
                if(ty < 0) {
                    ty = 0;
                }
                if(tx > 200) {
                    tx = 200;
                }
                if(ty > 200) {
                    ty = 200;
                }
                
                //鼠标移入小框 和 放大镜显示
                cover.style.display = 'block'
                deteil.style.display = 'block'
                //显示放大镜的位置
                deteil.style.backgroundPosition = tx / 200 * 100 + '% ' + ty / 200 * 100 + '%';
                //小框的位置
                cover.style.left = tx + 'px';
                cover.style.top = ty + 'px';
            })
            //鼠标移出  不显示
            pic.addEventListener('mouseout', function() {
                cover.style.display = 'none'
                deteil.style.display = 'none'
            });

 

posted @ 2019-09-27 15:22  暖文  阅读(174)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3