获取一个元素到页面的距离,限定区域拖拽元素,放大镜布局实现淘宝放大镜

获取一个元素到页面的距离
		 function offset(dom) {
		            // 返回一个对象
		            var obj = {
		                left: 0,
		                top: 0
		            }
            // 先让这个对象加上 dom的自己得到定位父元素的距离
            obj.left = dom.offsetLeft;
            obj.top = dom.offsetTop;
            // 判定浏览器是否是IE8 
            var isIE8 = window.navigator.userAgent.indexOf("MSIE 8") != -1;
            // 循环往上走 累加每一个offsetParent的offsetLeft和clientLeft 
            // 加每一个offsetParent的offsetTop和clientTop
            var offsetParent = dom.offsetParent;
            while (offsetParent != document.body) {
                if (isIE8) {
                    obj.left += offsetParent.offsetLeft;
                    obj.top += offsetParent.offsetTop;
                } else {
                    obj.left += offsetParent.offsetLeft + offsetParent.clientLeft;
                    obj.top += offsetParent.offsetTop + offsetParent.clientTop;
                }
                offsetParent = offsetParent.offsetParent;
            }
            return obj;
        }
限定区域拖拽对象:
				<style>
				        * {
				            margin: 0;
				            padding: 0;
				        }
				        .box {
				            position: absolute;
				            width: 100px;
				            height: 100px;
				            background-color: red;
				        }
				        #wrap {
				            width: 400px;
				            height: 400px;
				            position: absolute;
				            top: 100px;
				            left: 100px;
				            border: 1px solid #000;
				        }
				    </style>
				</head>
				<body>
				    <div id="wrap">
				        <div class="box"></div>
				    </div>
				    <script>
				        // 获取元素
				        var wrap = document.getElementById("wrap");
				        var box = document.querySelector(".box");
				        // 绑定鼠标按下事件
				        box.onmousedown = function(e) {
				            /* 鼠标按下时 记住鼠标此时的在视口中的位置 */
				            var x = e.clientX;
				            var y = e.clientY;
				            // 记住鼠标按下时 元素的定位值
				            var left = box.offsetLeft;
				            var top = box.offsetTop;
				            console.log("鼠标按下");
				            document.onmousemove = function(e) {
				                var moved_x = e.clientX;
				                var moved_y = e.clientY;
				                var resultX = left + moved_x - x;
				                var resultY = top + moved_y - y;
				                if (resultX < 0) {
				                    resultX = 0;
				                } else if (resultX > wrap.clientWidth - box.clientWidth) {
				                    resultX = wrap.clientWidth - box.clientWidth;
				                }
				                if (resultY < 0) {
				                    resultY = 0;
				                } else if (resultY > wrap.clientHeight - box.clientHeight) {
				                    resultY = wrap.clientHeight - box.clientHeight;
				                }                
				                box.style.left =resultX + "px";
				                box.style.top = resultY + "px";
				                /* 鼠标移动之后记住鼠标移动后的位置 这两个值相减 就是鼠标移动的距离 这个距离就是box元素相对于鼠标按下时定位值的变化距离  */
				            }
				        }
				        document.onmouseup = function() {
				            document.onmousemove = null;
				        }   
				    </script>
				</body>
放大镜布局
				<style>
				        * {
				            margin: 0;
				            padding: 0;
				        }
				        .box {
				            position: absolute;
				            top: 0;
				            left: 0;
				            width: 230px;
				            height: 230px;
				            background: url(./images/dotpng.png)
				        }
				        #wrap {
				            width: 430px;
				            height: 430px;
				            position: absolute;
				            top: 100px;
				            left: 100px;
				            border: 1px solid #000;
				        }
				        #bigArea {
				            width: 430px;
				            height: 430px;
				            position: absolute;
				            top: 100px;
				            left: 550px;
				            background: url(./images/big.jpg);
				            border: 1px solid #000;
				        }
				    </style>
				</head>
				<body>
				    <div id="wrap">
				        <img src="./images/small.jpg" >
				        <div class="box"></div>
				    </div>
				 <div id="bigArea">
				    </div>
				    <script>
				        // 获取元素
				        var wrap = document.getElementById("wrap");
				        var box = document.querySelector(".box");
				        var bigArea = document.getElementById("bigArea");
				        // 计算系数  小的镜片 比 右边的放大镜
				        var r = (wrap.clientWidth - box.clientWidth) / (800 - bigArea.clientWidth);
				        // 绑定鼠标按下事件
				        box.onmousedown = function(e) {
				            /* 鼠标按下时 记住鼠标此时的在视口中的位置 */
				            var x = e.clientX;
				            var y = e.clientY;
				            // 记住鼠标按下时 元素的定位值
				            var left = box.offsetLeft;
				            var top = box.offsetTop;
				            console.log("鼠标按下");
				            document.onmousemove = function(e) {
				                var moved_x = e.clientX;
				                var moved_y = e.clientY;
				
				                var resultX = left + moved_x - x;
				                var resultY = top + moved_y - y;
				                if (resultX < 0) {
				                    resultX = 0;
				                } else if (resultX > wrap.clientWidth - box.clientWidth) {
				                    resultX = wrap.clientWidth - box.clientWidth;
				                }
				                if (resultY < 0) {
				                    resultY = 0;
				                } else if (resultY > wrap.clientHeight - box.clientHeight) {
				                    resultY = wrap.clientHeight - box.clientHeight;
				                }                
				                box.style.left = resultX + "px";
				                box.style.top = resultY + "px";
				                /* 鼠标移动之后记住鼠标移动后的位置 这两个值相减 就是鼠标移动的距离 这个距离就是box元素相对于鼠标按下时定位值的变化距离  */
				                bigArea.style.backgroundPositionX = -resultX / r + "px";
				                bigArea.style.backgroundPositionY = -resultY / r+ "px";
				            }
				        }
				        document.onmouseup = function() {
				            document.onmousemove = null;
				        }
				    </script>
				</body>
淘宝放大镜

代码如下:

					<style>
					        * {
					            margin: 0;
					            padding: 0;
					        }
					        .box {
					            display: none;
					            position: absolute;
					            top: 0;
					            left: 0;
					            width: 230px;
					            height: 230px;
					            /* background-color: #0f0; */
					
					            background: url(./images/dotpng.png)
					        }
					        #wrap {
					            width: 430px;
					            height: 430px;
					            position: absolute;
					            top: 100px;
					            left: 100px;
					            border: 1px solid #000;
					        }
					        #bigArea {
					            display: none;
					            width: 430px;
					            height: 430px;
					            position: absolute;
					            top: 100px;
					            left: 550px;
					            background: url(./images/big.jpg);
					            border: 1px solid #000;
					        }
					    </style>
					</head>
					<body>
					    <div id="wrap">
					        <img src="./images/small.jpg" >
					        <div class="box"></div>
					    </div>
					
					    <div id="bigArea">
					    </div>
					    <script>
					        // 获取元素
					        var wrap = document.getElementById("wrap");
					        var box = document.querySelector(".box");
					        var bigArea = document.getElementById("bigArea");
					        console.log(bigArea)
					        // 鼠标进入图片区域 才显示镜片和放大镜
				       		 wrap.onmouseenter = function() {
				            box.style.display = "block";
				            bigArea.style.display = "block";
				            var r = (wrap.clientWidth - box.clientWidth) / (800 - bigArea.clientWidth);
				            // 绑定mousemove事件
				            document.onmousemove = function(e) {
				                // 计算鼠标在页面中的距离
				                var mouseX =  e.pageX;
				                var mouseY = e.pageY;
				                // 计算元素在视口中的距离
				                var elementX = offset(wrap).left;
				                var elementY = offset(wrap).top;
				                // 计算鼠标在元素中的距离
				                var resultX = mouseX - elementX - wrap.clientLeft - box.clientWidth / 2;
				                var resultY = mouseY - elementY - wrap.clientTop - box.clientHeight / 2;
				                if (resultX < 0) {
				                    resultX = 0;
				                } else if (resultX > wrap.clientWidth - box.clientWidth) {
				                    resultX = wrap.clientWidth - box.clientWidth;
				                }
				                if (resultY < 0) {
				                    resultY = 0;
				                } else if (resultY > wrap.clientHeight - box.clientHeight) {
				                    resultY = wrap.clientHeight - box.clientHeight;
				                }
				                box.style.left = resultX + "px";
				                box.style.top = resultY + "px";
				                bigArea.style.backgroundPositionX = -resultX / r + "px";
				                bigArea.style.backgroundPositionY = -resultY / r + "px";
				            }
				        }
				        wrap.onmouseleave = function() {
				            box.style.display = "none";
				            bigArea.style.display = "none";
				        }
			        function offset(dom) {
				            // 返回一个对象
				            var obj = {
				                left: 0,
				                top: 0
				            }
				            // 先让这个对象加上 dom的自己得到定位父元素的距离
				            obj.left = dom.offsetLeft;
				            obj.top = dom.offsetTop;
				            // 判定浏览器是否是IE8 
				            var isIE8 = window.navigator.userAgent.indexOf("MSIE 8") != -1;
				            // 循环往上走 累加每一个offsetParent的offsetLeft和clientLeft 
				            // 加每一个offsetParent的offsetTop和clientTop
				            var offsetParent = dom.offsetParent;
				            while (offsetParent != document.body) {
				                if (isIE8) {
				                    obj.left += offsetParent.offsetLeft;
				                    obj.top += offsetParent.offsetTop;
				                } else {
				                    obj.left += offsetParent.offsetLeft + offsetParent.clientLeft;
				                    obj.top += offsetParent.offsetTop + offsetParent.clientTop;
				                }
				                offsetParent = offsetParent.offsetParent;
				            }
				            return obj;
				        }
				    </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值