如何用原生javascript实现放大镜效果_html/css_WEB-ITnose
随着科技的发展,网购已成为大家生活中必不可少的一个模式,各种电商平台也如雨后春笋般涌现出来,今天我们就来用原生js来实现类似淘宝选购物品时的放大镜效果.
这里要用到大小两张图片,我选取的是800x800和350x350大小的两张图片
图片来源于网络
首先写出html和css样式
html部分
<body> <div class="min"> <img src="/static/imghw/default1.png" data-src="img/1.jpg" class="lazy" / alt="如何用原生javascript实现放大镜效果_html/css_WEB-ITnose" > <div class="fd"></div> </div> <div class="max"><img src="/static/imghw/default1.png" data-src="img/2.jpg" class="lazy" / alt="如何用原生javascript实现放大镜效果_html/css_WEB-ITnose" ></div> </body>
原理是创建min和max两个区域,将小图img/1.jpg和创建的放大镜divfd放到min中,将大图img/2.jpg放到max中
css样式部分
<style type="text/css"> .min{ width: 350px; height: 350px; border: 1px solid black; float: left; position: relative; } .max{ width: 350px; height: 350px; border: 1px solid black; float: left; display: none; overflow: hidden; position: relative; } .max img{ position: absolute; margin: 0 auto; } .fd{ width: 153.125px; height: 153.125px; background-color: skyblue; opacity: 0.3; position: absolute; top: 0; left: 0; display: none; }
这里需要强调的是 2.放大镜的宽高,如果按照我选的尺寸的两张图宽高必须为153.125px,否则会出现左侧所选区域和右侧显示区域不能完全吻合的情况; 3.大图的父级max定义的框尺寸为什么比里面的图片小?(这里的框相当于一个窗户,里面的图片相当于窗子一面的物体,无论里面的物体多大也只能显示出窗子的尺寸) 4.当鼠标放在小图外区域时,大图和放大镜无显示,所以开始置max和fd里display:none;
为了显示效果我们先把display:none注掉,此时效果如下
样式图
js部分
首先分析逻辑顺序
定义变量1.鼠标覆盖显示max和fd2.确定放大镜的移动范围(不能出min)3.max的对应显示然后按顺序书写代码
<script type="text/javascript"> var min = document.querySelector(".min"), max = document.querySelector(".max"), img = document.querySelector(".max img"), fd = document.querySelector(".fd");
min.onmouseover = function(){ //1.鼠标覆盖显示max和fd max.style.display = "block"; fd.style.display = "block"; } //离开时隐藏 min.onmouseout= function(){ max.style.display = "none"; fd.style.display = "none"; } //2.fd的移动范围 min.onmousemove = function(){ //鼠标触摸的点 var x = event.clientX-min.offsetLeft-fd.offsetWidth/2; var y = event.clientY-min.offsetTop-fd.offsetHeight/2; //最大移动距离 var maxX = min.clientWidth-fd.offsetWidth; var maxY = min.clientHeight-fd.offsetHeight; //边界判断 if(x<=0){ x=0; }else if(x>=maxX){ x=maxX; } if(y<=0){ y=0; }else if(y>=maxY){ y=maxY; } //fd的位置 fd.style.left = x+"px"; fd.style.top = y+"px"; //fd/min = max/img //移动比例 var yidongX = x/maxX; var yidongY = y/maxY; //移动 //3.max的对应显示 // 对于大图而言 放大镜在小图上移动的比例 相当于img在可显示区域上移动的比例 放大镜右移等于图片左移 // 也就是本质上为img - max 然而需要负值 则*-1 简化后 为max-img img.style.left = yidongX * (max.clientWidth - img.offsetWidth) + "px"; img.style.top = yidongY * (max.clientHeight - img.offsetHeight) + "px"; }</script>
最后根据需求完善即可实现效果如下
1.gif

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.
