Home Web Front-end JS Tutorial Using native JavaScript to achieve a magnifying glass effect

Using native JavaScript to achieve a magnifying glass effect

Jun 07, 2018 am 11:56 AM
javascript magnifier

This article mainly introduces the simple magnifying glass effect implemented by native JavaScript, involving implementation techniques related to javascript event response and dynamic operation of page element attributes. Friends in need can refer to it

The examples in this article describe the implementation of native JavaScript simple magnifying glass effect. Share it with everyone for your reference, the details are as follows:

Principle: In fact, the so-called enlargement is to prepare two identical pictures, except for the different sizes. Moving the mouse to different positions will display the image content corresponding to the large image.

Complete code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <title>放大镜效果</title>

</head>

<body>

 <p id="wrap" style="position: relative;width: 900px;margin: 0 auto;text-align: center;">

  <p id="smallImg" style="width: 400px;height: 400px; position: relative;z-index: 1;">

   <img src="small.jpg" style="width: 400px;height: 400px;"/>

   <span id="filter" style="width: 200px;height: 200px;background-color: blue;opacity: 0.1;position: absolute;top: 0;left: 0; z-index: 2;cursor: move;display: none;">

   <span>

  </p>

  <p id="bigImg" style="width: 400px;height: 400px;overflow: hidden;position: absolute;right: 0px;top: 0;display: none;">

   <img src="large.jpg" style="width: 800px;height:800px; position: absolute;left: 0;top: 0;">

  </p>

 </p>

 <script type="text/javascript">

  var filter = document.getElementById('filter');

  var smallImg = document.getElementById('smallImg');

  var bigImg = document.getElementById('bigImg');

  var wrap = document.getElementById('wrap');

  var largeImgs = bigImg.getElementsByTagName('img')[0];

  smallImg.onmouseover = function(){

   bigImg.style.display = "inline-block";

   filter.style.display = "inline-block";

  }

  smallImg.onmousemove = function(event){

   var event = event || window.event;

   var mouseleft = event.clientX - wrap.offsetLeft;

   var mousetop = event.clientY - wrap.offsetTop;

   var left = mouseleft<smallImg.offsetWidth/4?0:mouseleft>smallImg.offsetWidth*3/4?smallImg.offsetWidth/2:(mouseleft - filter.offsetWidth/2);

   var top = mousetop<smallImg.offsetHeight/4?0:mousetop>smallImg.offsetHeight*3/4?smallImg.offsetHeight/2:(mousetop - filter.offsetWidth/2);

   filter.style.left = left + "px";

   filter.style.top = top +"px";

   largeImgs.style.left = "-" + left*bigImg.offsetWidth/smallImg.offsetWidth + "px";

   largeImgs.style.top = "-" + top*bigImg.offsetHeight/smallImg.offsetHeight + "px";

  }

  smallImg.onmouseout = function(){

   bigImg.style.display = "none";

   filter.style.display = "none";

  }

 </script>

</body>

</html>

Copy after login

Operation effect:

The above is what I compiled for everyone. I hope that in the future It will be helpful to everyone.

Related articles:

About the use of Material in Angular2 (detailed tutorial)

How to implement a circular Nodelist Dom list using JS

How to achieve text color change after click in Vue

The above is the detailed content of Using native JavaScript to achieve a magnifying glass effect. For more information, please follow other related articles on the PHP Chinese website!

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 Article Tags

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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

How to use insertBefore in javascript

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

See all articles