• 模拟淘宝放大镜小例子


     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'
                });
  • 相关阅读:
    linux下创建virtualenv时指定python版本
    Centos7系统如何不重启系统识别新添加的硬盘?
    centos7系统下hostname解析
    Linux之shell脚本for、while、case语句的高级用法
    Linux自制编译内核
    Centos7系统详细的启动流程
    cpio的用法
    Linux之删除带有空格的文件(而不是目录)
    Linux之特殊的环境变量IFS以及如何删除带有空格的目录
    zabbix使用自定义脚本监控内存
  • 原文地址:https://www.cnblogs.com/randon/p/11598214.html
Copyright © 2020-2023  润新知