Home Web Front-end JS Tutorial Magnifying glass effect implemented by native js

Magnifying glass effect implemented by native js

Oct 15, 2016 am 09:32 AM

这是我用原生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(&#39;#box ul li img&#39;);
for(var i=0; i<imgList.length; i++){
var img = imgList[i];
img.onmouseover = changeThumbImg;
}
function changeThumbImg(){
var previousHovered = document.querySelector(&#39;.hoveredThumb&#39;);
if(previousHovered==null){
this.className = &#39;hoveredThumb&#39;; 
changeMediumImg(this.src);	//修改中等图片的src
}else if(previousHovered!=this){
previousHovered.removeAttribute(&#39;class&#39;);
this.className = &#39;hoveredThumb&#39;;
changeMediumImg(this.src);	//修改中等图片的src
}
}
function changeMediumImg(thumbSrc){
var dotIndex = thumbSrc.lastIndexOf(&#39;.&#39;);
var prefix = thumbSrc.substring(0, dotIndex);
var suffix = thumbSrc.substring(dotIndex);
var mediumSrc = prefix + &#39;-m&#39;+suffix;
document.getElementById(&#39;mediumImg&#39;).src = mediumSrc;
}
/**
为放大镜jing添加鼠标移动事件
**/
document.querySelector(&#39;#mian&#39;).onmousemove = 
function(event){
var x = event.offsetX;	//事件相对于事件源的偏移量
var y = event.offsetY;	
var jing = document.getElementById(&#39;jing&#39;);	
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+&#39;px&#39;;
jing.style.top = top+&#39;px&#39;;
//修改大图的位置/
var largeImg = document.getElementById(&#39;largeImg&#39;);
largeImg.style.left = (-left*largeImg.width/350) + &#39;px&#39;;
largeImg.style.top = (-top*largeImg.height/350)+&#39;px&#39;;
}
document.querySelector(&#39;#mian&#39;).onmouseover = function(){
//显示jing
var jing = document.getElementById(&#39;jing&#39;);
jing.style.display = &#39;block&#39;;
var largeImgBox = document.getElementById(&#39;largeImgBox&#39;);
largeImgBox.style.display = &#39;block&#39;;
//获取当前要显示的大图的src
var src = document.querySelector(&#39;#mediumImg&#39;).src;
var dotIndex = src.lastIndexOf(&#39;.&#39;);
var prefix = src.substring(0, dotIndex-2);
var suffix = src.substring(dotIndex);
src = prefix + &#39;-l&#39;+ suffix;
var largeImg = document.getElementById(&#39;largeImg&#39;);
largeImg.src = src;
largeImg.style.display=&#39;block&#39;;
}
document.querySelector(&#39;#mian&#39;).onmouseout = 
function(){
//除去jing
var jing = document.getElementById(&#39;jing&#39;);
jing.style.display = &#39;none&#39;;
//除去大图显示区
document.getElementById(&#39;largeImgBox&#39;).style.display = &#39;none&#39;;
}
</script>
</body>
</html>
Copy after login


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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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 should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

Can PowerPoint run JavaScript? Can PowerPoint run JavaScript? Apr 01, 2025 pm 05:17 PM

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles