js实现图片放大镜功能,简单明了

请添加图片描述
写购物项目的时候,需要放大图片,这里用js写了一个方法,鼠标悬浮的时候放大当前图片

这个是class写法

<!--
 * @Descripttion: 
 * @Author: 苍狼一啸八荒惊
 * @LastEditTime: 2024-07-10 09:41:34
 * @LastEditors: 夜空苍狼啸
-->

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>商品详情</title>
  <!-- <link rel="stylesheet" href="./css/detail.css"> -->
  <style>
    .product_wrapper {
      width: 1200px;
      margin: 0 auto;
      font: 12px "Hiragino Sans GB", "Verdana", "Simsun";
      background-color: #fff;
    }


    .product_wrapper .detail {
      overflow: hidden;
    }

    .product_wrapper .detail .list_detail {
      width: 320px;
      height: 320px;
      float: left;
      padding: 0 20px 30px 0;
      position: relative;
    }

    .product_wrapper .detail .list_detail .list_detail_1 {
      width: 280px;
      height: 280px;
      position: relative;
    }

    .product_wrapper .detail .list_detail .list_detail_1>img {
      width: 100%;
      height: 100%;
    }

    .product_wrapper .detail .list_detail .list_detail_1 .mask {
      width: 150px;
      height: 150px;
      background: url(../img/zoom_pup.png);
      opacity: 0.5;
      position: absolute;
      left: 30px;
      top: 40px;
      pointer-events: none;
      display: none;
    }

    .product_wrapper .detail .list_detail .list_detail_2 {
      position: absolute;
      left: 0;
      bottom: 0;
      display: flex;
      justify-content: space-evenly;
      align-items: center;
    }

    .product_wrapper .detail .list_detail .list_detail_2>img {
      width: 54px;
      height: 54px;
      margin-right: 50px;
      cursor: pointer;
    }

    .product_wrapper .detail .list_detail .list_detail_2 .active {
      box-shadow: 0 0 8px rgb(255, 255, 255) inset, 0 0 8px red;
    }

    .product_wrapper .detail .list_detail .list_detail_enlarge {
      width: 480px;
      height: 480px;
      position: absolute;
      position: absolute;
      left: 90%;
      top: 0;
      overflow: hidden;
      display: none;
      z-index: 999;
    }

    .product_wrapper .detail .list_detail .list_detail_enlarge>img {
      display: block;
      width: 800px;
      height: 800px;
      position: absolute;
      left: 0;
      top: 0;
    }
  </style>
</head>

<body>


  <div class="product_wrapper">


    <div class="detail hover">
      <div class="list_detail" id="box">
        <!-- 放大镜 -->
        <div class="list_detail_1">
          <img src="./img/lx1.jpg" alt="">
          <div class="mask"></div>
        </div>
        <div class="list_detail_2">
          <img src="./img/lx1.jpg" class="active" alt="" data-show="./img/lx1.jpg" data-bg="./img/lx1.jpg">
          <img src="./img/lx2.jpg" alt="" data-show="./img/lx2.jpg" data-bg="./img/lx2.jpg">
          <img src="./img/lx3.jpg" alt="" data-show="./img/lx3.jpg" data-bg="./img/lx3.jpg">
        </div>
        <div class="list_detail_enlarge enlarge">
          <!-- <img src="./img/lx1.jpg" alt=""> -->
          <img src="http://img3m2.ddimg.cn/27/17/25583112-1_u_11.jpg" alt="">
        </div>


      </div>


    </div>
  </div>

  <!-- 引入放大镜 -->
  <!-- <script src="./js/detail.js"></script> -->
  <script>

    // 放大镜
    class Enlarge {
      constructor(select) {
        // 0-1. 获取到范围元素, 目的是为了让所有内容都出现在这里面
        this.ele = document.querySelector(select)
        this.show = this.ele.querySelector('.list_detail_1')
        this.mask = this.ele.querySelector('.mask')
        this.list = this.ele.querySelector('.list_detail_2')
        this.enlarge = this.ele.querySelector('.enlarge')
        this.bg = this.enlarge.firstElementChild
        // 需要一些尺寸数据
        this.show_w = this.show.clientWidth
        this.show_h = this.show.clientHeight
        // 非行内样式获取
        this.mask_w = parseInt(window.getComputedStyle(this.mask).width)
        this.mask_h = parseInt(window.getComputedStyle(this.mask).height)
        this.bg_w = parseInt(window.getComputedStyle(this.bg).width)
        this.bg_h = parseInt(window.getComputedStyle(this.bg).height)
        // 在这里调用函数来启动
        this.setScale()
        this.overOut()
        this.listChange()
        this.move()
      }
      // 计算数值
      setScale() {
        // 1. 计算数值
        this.enlarge_w = this.mask_w * this.bg_w / this.show_w
        this.enlarge_h = this.mask_h * this.bg_h / this.show_h

        // 2. 给 this.enlarge 赋值
        this.enlarge.style.width = this.enlarge_w + 'px'
        this.enlarge.style.height = this.enlarge_h + 'px'
      }
      // 鼠标经过显示遮罩层, 图片放大
      overOut() {
        this.show.addEventListener('mouseover', () => {
          this.mask.style.display = 'block'
          this.enlarge.style.display = 'block'
        })

        this.show.addEventListener('mouseout', () => {
          this.mask.style.display = 'none'
          this.enlarge.style.display = 'none'
        })
      }
      // tab 切换
      listChange() {
        this.list.addEventListener('click', e => {
          // 处理事件对象兼容
          e = e || window.event
          // 处理事件目标兼容
          const target = e.target || e.srcElement

          // 判断点击的是 img 标签
          if (target.nodeName !== 'IMG') return

          // 1. 切换 img 类名
          for (let i = 0; i < this.list.children.length; i++) {
            this.list.children[i].classList.remove('active')
          }
          target.classList.add('active')

          // 2. 切换 show里面img 和 bg 的 src
          const showUrl = target.dataset.show
          const bgUrl = target.dataset.bg

          this.show.firstElementChild.src = showUrl
          this.bg.src = bgUrl
        })
      }
      // 鼠标移动
      move() {
        // 1. 绑定事件
        this.show.addEventListener('mousemove', e => {
          e = e || window.event

          // 2. 获取光标坐标点
          let x = e.offsetX - this.mask_w / 2
          let y = e.offsetY - this.mask_h / 2

          // 3. 边界值判断
          if (x <= 0) x = 0
          if (y <= 0) y = 0
          if (x >= this.show_w - this.mask_w) x = this.show_w - this.mask_w
          if (y >= this.show_h - this.mask_h) y = this.show_h - this.mask_h

          this.mask.style.left = x + 'px'
          this.mask.style.top = y + 'px'

          const bg_x = x * this.enlarge_w / this.mask_w * -1
          const bg_y = y * this.enlarge_h / this.mask_h * -1

          this.bg.style.left = bg_x + 'px'
          this.bg.style.top = bg_y + 'px'
        })
      }
    }


    const enlarge = new Enlarge('#box')



  </script>

</body>

</html>

这个是函数写法

<!--
 * @Descripttion: 
 * @Author: 苍狼一啸八荒惊
 * @LastEditTime: 2024-07-10 09:41:34
 * @LastEditors: 夜空苍狼啸
-->

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>商品详情</title>
  <!-- <link rel="stylesheet" href="./css/detail.css"> -->
  <style>
    .product_wrapper {
      width: 1200px;
      margin: 0 auto;
      font: 12px "Hiragino Sans GB", "Verdana", "Simsun";
      background-color: #fff;
    }


    .product_wrapper .detail {
      overflow: hidden;
    }

    .product_wrapper .detail .list_detail {
      width: 320px;
      height: 320px;
      float: left;
      padding: 0 20px 30px 0;
      position: relative;
    }

    .product_wrapper .detail .list_detail .list_detail_1 {
      width: 280px;
      height: 280px;
      position: relative;
    }

    .product_wrapper .detail .list_detail .list_detail_1>img {
      width: 100%;
      height: 100%;
    }

    .product_wrapper .detail .list_detail .list_detail_1 .mask {
      width: 150px;
      height: 150px;
      background: url(../img/zoom_pup.png);
      opacity: 0.5;
      position: absolute;
      left: 30px;
      top: 40px;
      pointer-events: none;
      display: none;
    }

    .product_wrapper .detail .list_detail .list_detail_2 {
      position: absolute;
      left: 0;
      bottom: 0;
      display: flex;
      justify-content: space-evenly;
      align-items: center;
    }

    .product_wrapper .detail .list_detail .list_detail_2>img {
      width: 54px;
      height: 54px;
      margin-right: 50px;
      cursor: pointer;
    }

    .product_wrapper .detail .list_detail .list_detail_2 .active {
      box-shadow: 0 0 8px rgb(255, 255, 255) inset, 0 0 8px red;
    }

    .product_wrapper .detail .list_detail .list_detail_enlarge {
      width: 480px;
      height: 480px;
      position: absolute;
      position: absolute;
      left: 90%;
      top: 0;
      overflow: hidden;
      display: none;
      z-index: 999;
    }

    .product_wrapper .detail .list_detail .list_detail_enlarge>img {
      display: block;
      width: 800px;
      height: 800px;
      position: absolute;
      left: 0;
      top: 0;
    }
  </style>
</head>

<body>


  <div class="product_wrapper">


    <div class="detail hover">
      <div class="list_detail" id="box">
        <!-- 放大镜 -->
        <div class="list_detail_1">
          <img src="./img/lx1.jpg" alt="">
          <div class="mask"></div>
        </div>
        <div class="list_detail_2">
          <img src="./img/lx1.jpg" class="active" alt="" data-show="./img/lx1.jpg" data-bg="./img/lx1.jpg">
          <img src="./img/lx2.jpg" alt="" data-show="./img/lx2.jpg" data-bg="./img/lx2.jpg">
          <img src="./img/lx3.jpg" alt="" data-show="./img/lx3.jpg" data-bg="./img/lx3.jpg">
        </div>
        <div class="list_detail_enlarge enlarge">
          <img src="./img/lx1.jpg" alt="">
        </div>


      </div>


    </div>
  </div>

  <!-- 引入放大镜 -->
  <!-- <script src="./js/detail.js"></script> -->
  <script>
    /*
 * @Descripttion: 
 * @Author: 苍狼一啸八荒惊
 * @LastEditTime: 2024-07-10 10:18:57
 * @LastEditors: 夜空苍狼啸
 */

// 放大镜


// 0-1. 获取到范围元素, 目的是为了让所有内容都出现在这里面
let ele = document.querySelector('#box')
let show = ele.querySelector('.list_detail_1')
let mask = ele.querySelector('.mask')
let list = ele.querySelector('.list_detail_2')
let enlarge = ele.querySelector('.enlarge')
let bg = enlarge.firstElementChild
// 需要一些尺寸数据
let show_w = show.clientWidth
let show_h = show.clientHeight
// 非行内样式获取
let mask_w = parseInt(window.getComputedStyle(mask).width)
let mask_h = parseInt(window.getComputedStyle(mask).height)
let bg_w = parseInt(window.getComputedStyle(bg).width)
let bg_h = parseInt(window.getComputedStyle(bg).height)


// 计算数值
// 1. 计算数值
enlarge_w = mask_w * bg_w / show_w
enlarge_h = mask_h * bg_h / show_h

// 2. 给 enlarge 赋值
enlarge.style.width = enlarge_w + 'px'
enlarge.style.height = enlarge_h + 'px'

// 鼠标经过显示遮罩层, 图片放大

show.addEventListener('mouseover', () => {
    mask.style.display = 'block'
    enlarge.style.display = 'block'
})

show.addEventListener('mouseout', () => {
    mask.style.display = 'none'
    enlarge.style.display = 'none'
})

// tab 切换

list.addEventListener('click', e => {
    // 处理事件对象兼容
    e = e || window.event
    // 处理事件目标兼容
    const target = e.target || e.srcElement

    // 判断点击的是 img 标签
    if (target.nodeName !== 'IMG') return

    // 1. 切换 img 类名
    for (let i = 0; i < list.children.length; i++) {
        list.children[i].classList.remove('active')
    }
    target.classList.add('active')

    // 2. 切换 show里面img 和 bg 的 src
    const showUrl = target.dataset.show
    const bgUrl = target.dataset.bg

    show.firstElementChild.src = showUrl
    bg.src = bgUrl
})

// 鼠标移动

// 1. 绑定事件
show.addEventListener('mousemove', e => {
    e = e || window.event

    // 2. 获取光标坐标点
    let x = e.offsetX - mask_w / 2
    let y = e.offsetY - mask_h / 2

    // 3. 边界值判断
    if (x <= 0) x = 0
    if (y <= 0) y = 0
    if (x >= show_w - mask_w) x = show_w - mask_w
    if (y >= show_h - mask_h) y = show_h - mask_h

    mask.style.left = x + 'px'
    mask.style.top = y + 'px'

    const bg_x = x * enlarge_w / mask_w * -1
    const bg_y = y * enlarge_h / mask_h * -1

    bg.style.left = bg_x + 'px'
    bg.style.top = bg_y + 'px'
})







  </script>

</body>

</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/789788.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【CSS in Depth 2 精译】2.4 视口的相对单位

当前内容所在位置 第一章 层叠、优先级与继承第二章 相对单位 2.1 相对单位的威力2.2 em 与 rem2.3 告别像素思维2.4 视口的相对单位 ✔️2.5 无单位的数值与行高2.6 自定义属性2.7 本章小结 2.4 视口的相对单位 前面介绍过的 em 和 rem 是相对于 font-size 定义的&#xff0…

【Pikachu靶场】安装部署通关详解超详细!!!(持续更新)

安装部署 Pikachu靶场&#xff0c;是一个带有漏洞的Web应用系统&#xff0c;在这里包含了常见的web安全漏洞。使用世界上最好的语言PHP进行开发-_-&#xff0c;数据库使用的是mysql&#xff0c;因此运行Pikachu你需要提前安装好"PHPMYSQL中间件&#xff08;如apache,ngin…

【HTML入门】第八课 - 链接的学习(二)

我们上一节学习了&#xff0c;链接的基本知识&#xff0c;有锚点&#xff0c;还有鼠标上移的title属性的作用&#xff0c;这一节&#xff0c;我们继续说链接的知识点。 目录 1 跳转本项目的网页 1.1 修改html文件名 1.2 新建index1.html文件 1.3 修改index1.html文件 1.4…

python7:装饰器

目录 1.调用外部程序os.system-阻塞式调用subprocess-python中的模块 2.装饰器前戏作用域&#xff08;1&#xff09;全局和局部-就近原则&#xff08;2&#xff09;嵌套作用域&#xff08;3&#xff09;内置作用域、变量 高阶函数&#xff1a;函数是最高级的对象&#xff08;1&…

几种不同的方式禁止IP访问网站(PHP、Nginx、Apache设置方法)

1、PHP禁止IP和IP段访问 <?//禁止某个IP$banned_ip array ("127.0.0.1",//"119.6.20.66","192.168.1.4");if ( in_array( getenv("REMOTE_ADDR"), $banned_ip ) ){die ("您的IP禁止访问&#xff01;");}//禁止某个IP段…

【Linux操作系统-测试】第三节.Linux 系统、网络信息、用户权限命令总结

文章目录 前言一、Linux 系统相关信息命令 1.1 df 命令--查看磁盘剩余 1.2 ps 命令--查看进程 1.3 top 命令--显示进程运行状态 1.4 kill 命令说明 -- 杀死进程二、Linux 网络信息命令 2.1 ping 命令--检查网络是否连通 2.1 ifconfig--显示网络设…

传知代码-图神经网络长对话理解(论文复现)

代码以及视频讲解 本文所涉及所有资源均在传知代码平台可获取 概述 情感识别是人类对话理解的关键任务。随着多模态数据的概念&#xff0c;如语言、声音和面部表情&#xff0c;任务变得更加具有挑战性。作为典型解决方案&#xff0c;利用全局和局部上下文信息来预测对话中每…

数据库-ubuntu环境下安装配置mysql

文章目录 什么是数据库&#xff1f;一、ubuntu环境下安装mysql二、配置mysql配置文件1.先登上root账号2.配置文件的修改 mysql和mysqld数据库的基础操作登录mysql创建数据库显示当前数据库使用数据库创建表插入students表数据打印students表数据select * from students; ![在这…

响应式布局下关于gird栅格布局的一些构思

1、传列数&#xff0c;根据列数计算元素容器宽度 好处是子元素可以写百分比宽度&#xff0c;不用固定某一种宽度&#xff0c;反正知道列数通过计算间距就能得到外层容器的宽度。 举个简单的例子&#xff1a; &#xff08;ps:以下用例皆在html中去模拟&#xff0c;就不另外起r…

零基础STM32单片机编程入门(十二) HC-SR04超声波模块测距实战含源码

文章目录 一.概要二.HC-SR04主要参数1.模块引脚定义2.模块电气参数3.模块通讯时序4.模块原理图 三.STM32单片机超声波模块测距实验四.CubeMX工程源代码下载五.小结 一.概要 HC-SR04超声波模块常用于机器人避障、物体测距、液位检测、公共安防、停车场检测等场所。HC-SR04超声波…

宏碁F5-572G-59K3笔记本笔记本电脑拆机清灰教程(详解)

1. 前言 我的笔记本开机比较慢&#xff0c;没有固态&#xff0c;听说最近固态比较便宜&#xff0c;就想入手一个&#xff0c;于是拆笔记本看一下有没有可以安的装位置。&#xff08;友情提示&#xff0c;在拆机之前记得洗手并擦干&#xff0c;以防静电损坏电源器件&#xff09…

视频号的视频,一键就下载了,方法全在这儿了!

居然还有人不知道&#xff1a;视频号里面的视频是没有地址的&#xff0c;只能有微信自带的浏览器中打开。 所以很多人在视频号找到想要的素材&#xff0c;却无法下载&#xff0c;表示很苦恼。 几天每天都有人群里求助&#xff1a;“求好心人帮我下载一下这个视频&#xff01;…

昇思学习打卡-14-ResNet50迁移学习

文章目录 数据集可视化预训练模型的使用部分实现 推理 迁移学习&#xff1a;在一个很大的数据集上训练得到一个预训练模型&#xff0c;然后使用该模型来初始化网络的权重参数或作为固定特征提取器应用于特定的任务中。本章学习使用的是前面学过的ResNet50&#xff0c;使用迁移学…

STM32 GPIO的工作原理

STM32的GPIO管脚有下面8种可能的配置:&#xff08;4输入 2 输出 2 复用输出) &#xff08;1&#xff09;浮空输入_IN_FLOATING 在上图上&#xff0c;阴影的部分处于不工作状态&#xff0c;尤其是下半部分的输出电路&#xff0c;实际上是与端口处于隔离状态。黄色的高亮部分显示…

在攻防演练中遇到的一个“有马蜂的蜜罐”

在攻防演练中遇到的一个“有马蜂的蜜罐” 有趣的结论&#xff0c;请一路看到文章结尾 在前几天的攻防演练中&#xff0c;我跟队友的气氛氛围都很好&#xff0c;有说有笑&#xff0c;恐怕也是全场话最多、笑最多的队伍了。 也是因为我们遇到了许多相当有趣的事情&#xff0c;其…

Mac窗口辅助管理工具:Magnet for mac激活版

magnet mac版是一款运行在苹果电脑上的一款优秀的窗口大小控制工具&#xff0c;拖拽窗口到屏幕边缘可以自动半屏&#xff0c;全屏或者四分之一屏幕&#xff0c;还可以设定快捷键完成分屏。这款专业的窗口管理工具当您每次将内容从一个应用移动到另一应用时&#xff0c;当您需要…

python学习-容器类型

列表 列表&#xff08;list&#xff09;是一种有序容器&#xff0c;可以向其中添加或删除任意元素. 列表数据类型是一种容器类型&#xff0c;列表中可以存放不同数据类型的值,代码示例如下&#xff1a; 列表中可以实现元素的增、删、改、查。 示例代码如下&#xff1a; 增 …

医疗器械网络安全 | 漏洞扫描、渗透测试没有发现问题,是否说明我的设备是安全的?

尽管漏洞扫描、模糊测试和渗透测试在评估系统安全性方面是非常重要和有效的工具&#xff0c;但即使这些测试没有发现任何问题&#xff0c;也不能完全保证您的医疗器械是绝对安全的。这是因为安全性的评估是一个多维度、复杂且持续的过程&#xff0c;涉及多个方面和因素。以下是…

AI提示词:打造爆款标题生成器

打开GPT输入以下内容&#xff1a; # Role 爆款标题生成器## Profile - author: 姜小尘 - version: 02 - LLM: Kimi - language: 中文 - description: 利用心理学和市场趋势&#xff0c;生成吸引眼球的自媒体文章标题。## Background 一个吸引人的标题是提升文章点击率和传播力…

优化爬虫体验:揭秘IP重复率过高问题解决方案

在当今信息爆炸的时代&#xff0c;网络中蕴藏着大量宝贵的数据&#xff0c;而爬虫技术成为我们提取这些数据的重要工具。然而&#xff0c;随着爬虫的广泛使用&#xff0c;IP重复率高的问题也随之而来。本篇博文将揭秘解决这一问题的关键方法——使用IP代理。 一、 IP高重复问题…