放大鏡效果

商城中常用的圖片放大效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="index.css">
    <style>
        *{margin: 0;padding: 0;}
        body{padding: 100px;}
        #demo{
            width: 400px;
            height: 250px;
            position: relative;
            border: 1px solid #ccc;
        }
        #small-box{
            position: relative;
            z-index: 1;
        }
        #small-box img{
            display: block;
        }
        #float-box{
            display: none;
            width: 160px;
            height: 100px;
            position: absolute;
            background-color: #ffc;
            border: 1px solid #ccc;
            opacity: 0.5;
            filter: alpha(opacity=50);
            cursor: move;
        }
        #mark{
            position: absolute;
            width: 400px;
            height: 250px;
            z-index: 10;
            cursor: move;
            background-color: #fff;
            opacity: 0;
            filter: alpha(opacity=0);
        }
        #big-box{
            display: none;
            position: absolute;
            top: 0;
            left: 460px;
            width: 768px;
            height: 512px;
            overflow: hidden;
            border: 1px solid #ccc;
            z-index: 1;
        }
        #big-box img{
            position: absolute;
            z-index: 5;
        }
    </style>
</head>
<body>
    <div id="demo">
        <div id="small-box">
            <div id="mark"></div>
            <div id="float-box"></div>
            ![](http://upload-images.jianshu.io/upload_images/1875545-af67ad614d82af67.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
        </div>
        <div id="big-box">
            ![](http://upload-images.jianshu.io/upload_images/1875545-aea54178745af3be.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
        </div>
    </div>

    <script>
        window.onload = function() {
            var objDemo = document.getElementById('demo');
            var objSmallBox = document.getElementById('small-box');
            var objMark = document.getElementById('mark');
            var objFloatBox = document.getElementById('float-box');
            var objBigBox = document.getElementById('big-box');
            var objBigBoxImage = objBigBox.getElementsByTagName('img')[0];

            objMark.onmouseover = function() {
                objFloatBox.style.display = 'block';
                objBigBox.style.display = 'block';
            }

            objMark.onmouseout = function() {
                objFloatBox.style.display = 'none';
                objBigBox.style.display = 'none';
            }

            objMark.onmousemove = function(e) {
                var _event = e || window.event;

                var left = _event.clientX - objDemo.offsetLeft - objSmallBox.offsetLeft - objFloatBox.offsetWidth / 2;
                var top = _event.clientY - objDemo.offsetTop - objSmallBox.offsetTop - objFloatBox.offsetHeight / 2;

                if (left < 0) {
                    left = 0;
                } else if (left > (objMark.offsetWidth - objFloatBox.offsetWidth)) {
                    left = objMark.offsetWidth - objFloatBox.offsetWidth;
                }

                if (top < 0) {
                    top = 0;
                } else if (top > (objMark.offsetHeight - objFloatBox.offsetHeight)) {
                    top = objMark.offsetHeight - objFloatBox.offsetHeight;
                }

                objFloatBox.style.left = left + 'px';
                objFloatBox.style.top = top + 'px';

                var percentX = left / (objMark.offsetWidth - objFloatBox.offsetWidth);
                var percentY = top / (objMark.offsetHeight - objFloatBox.offsetHeight);

                objBigBoxImage.style.left = -percentX * (objBigBoxImage.offsetWidth - objBigBox.offsetWidth) + 'px';
                objBigBoxImage.style.top = -percentY * (objBigBoxImage.offsetHeight - objBigBox.offsetHeight) + 'px';
            }
        }
    </script>
</body>
</html>

簡書會自動上傳外鏈圖片,所以這里代碼里面img標(biāo)簽里面對應(yīng)的換兩個地址【@換成.】,http://7xr9pe.com1.z0.glb.clouddn.com/small@jpg、http://7xr9pe.com1.z0.glb.clouddn.com/big@jpg
效果如下所示:

放大鏡預(yù)覽效果

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容