Table of Contents
html部分
css样式部分
js部分
Home Web Front-end HTML Tutorial 如何用原生javascript实现放大镜效果_html/css_WEB-ITnose

如何用原生javascript实现放大镜效果_html/css_WEB-ITnose

Jun 24, 2016 am 11:19 AM

随着科技的发展,网购已成为大家生活中必不可少的一个模式,各种电商平台也如雨后春笋般涌现出来,今天我们就来用原生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>
Copy after login

原理是创建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;                }
Copy after login

这里需要强调的是 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");
Copy after login
操作
    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>
Copy after login

最后根据需求完善即可实现效果如下

1.gif

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

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

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

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

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

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

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

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.

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

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

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; 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

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

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

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

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

See all articles