Magnifying glass effect implemented by native js
这是我用原生js写的放大镜效果,与各种各样的框架技术相比,我喜欢使用原生的js,在这里,想和大家一起谈谈原生和框架技术的理解与个人喜好。
<!DOCTYPE HTML> <html> <head> <title>js放大镜效果</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"/> <style> *{ margin:0px; padding:0px; border:none; list-style:none; } #box{ margin:80px auto; width: 352px; } #box p { width: 350px; height: 350px; border: 1px solid #ddd; margin-bottom: 5px; } #box p img { width:350px; height:350px; } #box h1 { width: 352px; height: 54px; overflow:hidden; position:relative; } #box h1 div { width:310px; height: 54px; margin:0px auto; position:relative; } #box h1 div ul { position:absolute; left: 0px; top: 0px; } #box h1 ul li { width: 62px; float: left; } #box h1 ul li img { width: 50px; height: 50px; padding: 1px; border: 1px solid #CECFCE; } #box h1 img.hoveredThumb{ border: 2px solid #e4393c; padding: 0; } /*中等大小的图片显示区域*/ p#medImgBox{ position: relative; } #jing{ position: absolute; left: 0; top: 0; width: 175px; height: 175px; border-radius:50%; background: #ffd; opacity: 0.7; display: none; } /*悬于图片/jing上方的专用于接收鼠标移动事件的 一个完全透明的层*/ #medImgBox #mian{ display: block; position: absolute; width: 100%; height: 100%; top: 0; left: 0; cursor: move; opacity: 0; } /** 大图显示区域 **/ #largeImgBox{ position:absolute; width: 175px; height: 175px; border-radius:50%; border: 1px solid #faa; top: 0px; left: 352px; overflow: hidden; display: none; } #largeImg{ display: none; position: absolute; } </style> </head> <body > <div id="box"> <p id="medImgBox"> <img src="/static/imghw/default1.png" data-src="images/product-s1-m.jpg" class="lazy" id="mediumImg" / alt="Magnifying glass effect implemented by native js" > <span id="jing"></span> <span id="mian"></span> <span id="largeImgBox"> <img src="/static/imghw/default1.png" data-src="images\product-s1.jpg" class="lazy" id="largeImg"/ alt="Magnifying glass effect implemented by native js" > </span> </p> <h1> <div> <ul id="list"> <li><img src="/static/imghw/default1.png" data-src="images\product-s1.jpg" class="lazy" / alt="Magnifying glass effect implemented by native js" ></li> <li><img src="/static/imghw/default1.png" data-src="images\product-s2.jpg" class="lazy" / alt="Magnifying glass effect implemented by native js" ></li> <li><img src="/static/imghw/default1.png" data-src="images\product-s3.jpg" class="lazy" / alt="Magnifying glass effect implemented by native js" ></li> <li><img src="/static/imghw/default1.png" data-src="images\product-s4.jpg" class="lazy" / alt="Magnifying glass effect implemented by native js" ></li> <li><img src="/static/imghw/default1.png" data-src="images\product-s1.jpg" class="lazy" / alt="Magnifying glass effect implemented by native js" ></li> </ul> </div> </h1> </div> <script> var imgList = document.querySelectorAll('#box ul li img'); for(var i=0; i<imgList.length; i++){ var img = imgList[i]; img.onmouseover = changeThumbImg; } function changeThumbImg(){ var previousHovered = document.querySelector('.hoveredThumb'); if(previousHovered==null){ this.className = 'hoveredThumb'; changeMediumImg(this.src); //修改中等图片的src }else if(previousHovered!=this){ previousHovered.removeAttribute('class'); this.className = 'hoveredThumb'; changeMediumImg(this.src); //修改中等图片的src } } function changeMediumImg(thumbSrc){ var dotIndex = thumbSrc.lastIndexOf('.'); var prefix = thumbSrc.substring(0, dotIndex); var suffix = thumbSrc.substring(dotIndex); var mediumSrc = prefix + '-m'+suffix; document.getElementById('mediumImg').src = mediumSrc; } /** 为放大镜jing添加鼠标移动事件 **/ document.querySelector('#mian').onmousemove = function(event){ var x = event.offsetX; //事件相对于事件源的偏移量 var y = event.offsetY; var jing = document.getElementById('jing'); var w = jing.offsetWidth; //jing的宽和高 var h = jing.offsetHeight; var left = x<w/2 ? 0 : (x-w/2); var top = y<h/2 ? 0 : (y-h/2); if(x>(w*2-w/2)){ left = w*2 - w; } if(y>(h*2-h/2)){ top = h*2 - h; } jing.style.left = left+'px'; jing.style.top = top+'px'; //修改大图的位置/ var largeImg = document.getElementById('largeImg'); largeImg.style.left = (-left*largeImg.width/350) + 'px'; largeImg.style.top = (-top*largeImg.height/350)+'px'; } document.querySelector('#mian').onmouseover = function(){ //显示jing var jing = document.getElementById('jing'); jing.style.display = 'block'; var largeImgBox = document.getElementById('largeImgBox'); largeImgBox.style.display = 'block'; //获取当前要显示的大图的src var src = document.querySelector('#mediumImg').src; var dotIndex = src.lastIndexOf('.'); var prefix = src.substring(0, dotIndex-2); var suffix = src.substring(dotIndex); src = prefix + '-l'+ suffix; var largeImg = document.getElementById('largeImg'); largeImg.src = src; largeImg.style.display='block'; } document.querySelector('#mian').onmouseout = function(){ //除去jing var jing = document.getElementById('jing'); jing.style.display = 'none'; //除去大图显示区 document.getElementById('largeImgBox').style.display = 'none'; } </script> </body> </html>

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

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Enhance Your Code Presentation: 10 Syntax Highlighters for Developers Sharing code snippets on your website or blog is a common practice for developers. Choosing the right syntax highlighter can significantly improve readability and visual appeal. T

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

This article presents a curated selection of over 10 tutorials on JavaScript and jQuery Model-View-Controller (MVC) frameworks, perfect for boosting your web development skills in the new year. These tutorials cover a range of topics, from foundatio

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was
