04 - 放大镜特效


HTML结构

<div class="box" id="box">
    <div id="smallBox">
        ![](images/pic001.jpg)
        <span id="mask"></span>
    </div>
    <div id="bigBox">
        ![](images/pic01.jpg)
    </div>
    <div id="list">
        ![](images/pic0001.jpg)
        ![](images/pic0002.jpg)
        ![](images/pic0003.jpg)
    </div>
</div>

Css样式

<style>
        * {
            margin: 0;
            padding: 0;
            border: 0;
        }

        img {
            vertical-align: top;
        }

        .box {
            width: 350px;
            height: 350px;
            margin: 50px;
            position: relative;
            border: 1px solid #cccccc;

        }

        #smallBox {
            width: 100%;
            height: 100%;
            position: relative;
            cursor: move;
        }

        #mask {
            width: 100px;
            height: 100px;
            background: rgba(255, 255, 0, .4);
            position: absolute;
            left: 0;
            top: 0;
            display: none;
        }

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

        #bigBox img {
            width: 800px;
            position: absolute;
            left: 0;
            top: 0;
        }

        #list {
            margin-top: 20px;
        }
</style>

Js代码

<script>
        window.onload = function () {
            // 1. 获取标签
            var box = document.getElementById('box');

            var smallbox = box.children[0];
            var mask = smallbox.children[1];
            var bigBox = box.children[1];
            var bigImg = bigBox.children[0];
            var list = box.children[2];
            var allLis = list.children;

            // 2. 鼠标监听事件
            smallbox.onmouseover = function () {
                // 2.1 显示隐藏的盒子
                mask.style.display = 'block';
                bigBox.style.display = 'block';

                // 2.2 鼠标移动跟随
                smallbox.onmousemove = function (event) {
                    var event = event || window.event;
                    var pointX = event.pageX - box.offsetLeft - mask.offsetWidth / 2;
                    var pointY = event.pageY - box.offsetTop - mask.offsetHeight / 2;

                    // 2.3 计算边缘值

                    if (pointX < 0) {
                        pointX = 0;
                    }
                    else if (pointX >= box.offsetWidth - mask.offsetWidth) {
                        pointX = box.offsetWidth - mask.offsetWidth - 1;
                    }

                    if (pointY < 0) {
                        pointY = 0;
                    }
                    else if (pointY >= box.offsetHeight - mask.offsetHeight) {
                        pointY = box.offsetHeight - mask.offsetHeight - 1;
                    }

                    // 2.4 赋值给小黄盒子
                    mask.style.left = pointX + 'px';
                    mask.style.top = pointY + 'px';

                    // 2.5 开始移动大图片
                    // smallX / bigX  = smallBox.width / 大图片的宽度
                    // bigX = smallX / (smallBox.width / 大图片的宽度)
                    bigImg.style.left = - pointX / (smallbox.offsetWidth / bigBox.offsetWidth) + 'px';
                    bigImg.style.top = - pointY / (smallbox.offsetHeight / bigBox.offsetHeight) + 'px';

                }

                // 隐藏显示的盒子
                smallbox.onmouseout = function () {
                    mask.style.display = 'none';
                    bigBox.style.display = 'none';
                }
            }

            // 3. 图片对应效果

            for (var i = 0; i < allLis.length; i ++) {
                (function (index) {
                    var img = allLis[i];
                    img.onmouseover = function () {
                        bigImg.src = 'images/pic0' + index + '.jpg'
                        smallbox.children[0].src = 'images/pic00' + index + '.jpg';
                    }
                }(i + 1));
            }
        }
</script>

特效展示

放大镜
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,680评论 1 92
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,254评论 25 709
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,196评论 4 61
  • 正则表达式:又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp...
    没说再见阅读 1,287评论 0 0
  • New York is 3 hours ahead of California, 纽约时间比加州时间早三个小时 b...
    Sancymissing阅读 3,207评论 0 0