当前位置: 首页 > news >正文

原生HTML放大镜

该放大区域用背景图片放大

<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title></title><style type="text/css">body {height: 1200px;background-color: lightskyblue;}ul {margin: 0;padding: 0;list-style: none;}.itemarea {position: relative;width: 500px;height: 680px;border: 1px black solid;margin: 50px auto;}.itemarea .pic {margin-bottom: 15px;}.itemarea img {width: 500px;height: 600px;}.itemarea .pic .cover {position: absolute;left: 0;top: 0;width: 200px;height: 200px;background-image: url(img/7.png);opacity: 0.6;display: none;}.itemarea .list {display: flex;}.itemarea .list li {margin: auto;}.itemarea .list img {display: block;width: 50px;height: 50px;}.itemarea .detail {position: absolute;top: 0;left: 500px;/* 此处为放大2倍,显示框的大小是遮阴框宽高的2倍 */width: 400px;height: 400px;display: none;border: 1px black solid;background: url(img/1.PNG);/* 此处放大2倍,背景图片的宽高是左边显示图片的2倍 */background-size: 1000px 1200px;/* background-size: 200%; 或者这样写*/}.itemarea .list .current {border: 2px green solid;}</style></head><body><div class="itemarea"><div class="pic"><img src="img/1.PNG"><div class="cover"></div></div><ul class="list"><li><img src="img/1.PNG"></li><li><img src="img/2.PNG"></li><li><img src="img/3.PNG"></li><li><img src="img/4.PNG"></li><li><img src="img/5.PNG"></li><li><img src="img/6.PNG"></li></ul><div class="detail"></div></div><script type="text/javascript">/* 需求1,鼠标放入图片时候,会动态修改图片地址2,鼠标放入大图,会动态修改右边图片位置2.1显示图片的放大镜,2.2显示右边效果*/var itemarea = document.querySelector(".itemarea");var list = document.querySelector(".list");/* 上面的大图片 */img = document.querySelector(".pic img");/* 所有的图片 */imgs = list.querySelectorAll("img");/* 主图片展示区域 */pic = document.querySelector(".itemarea .pic");/* 放大镜 */cover = document.querySelector(".cover");/* 放大的区域 */detail = document.querySelector(".detail");/* 监听事件,切换图片src */list.addEventListener("mousemove", function(e) {if (e.target.tagName == "IMG") {img.src = e.target.src;detail.style.backgroundImage = "url(" + e.target.src + ")";/* 遍历 所有边框都为空*/imgs.forEach(function(item) {item.className = "";})/* 选中的改变边框颜色*/e.target.className = "current";}})pic.addEventListener("mousemove", function(e) {/* 放大镜距离浏览器的距离 */var x = e.clientX;y = e.clientY;/* 图片框距离浏览器的距离 */cx = pic.getBoundingClientRect().left;cy = pic.getBoundingClientRect().top;tx = x - cx - 100;ty = y - cy - 100;if (tx < 0) {tx = 0;}if (ty < 0) {ty = 0;}/* 显示图片宽-遮阴框的宽 */if (tx >300) {tx = 300;}/* 显示图片高-遮阴框的高 */if (ty > 400) {ty = 400;}cover.style.left = tx + "px";cover.style.top = ty + "px";/* 根据遮阴框在盒子的移动距离百分比------对应放映框在大图片的移动距离百分比 *//*    tx,ty/遮阴框的极限范围 */detail.style.backgroundPosition = tx / 300 * 100 + "%" + ty / 400 * 100 + "%";})/* 移除隐藏 */itemarea.onmouseout = function() {cover.style.display = "none";detail.style.display = "none"}itemarea.onmouseover = function() {cover.style.display = "block";detail.style.display = "block";}</script></body>
</html>

效果如下:

在这里插入图片描述

http://www.lryc.cn/news/34951.html

相关文章:

  • C++——模板
  • Chapter2.1:线性表基础
  • Spring源码解析-Spring 循环依赖
  • 从零开始学架构——架构设计的目的
  • Python 异步: 异步生成器(16)
  • .net6 web api使用EF Core,根据model类自动生成表
  • 计算机科学导论笔记(五)
  • 通过命令打Java可执行jar包
  • java基础系列(九) 接口和抽象类
  • Docker启动问题docker is starting…
  • Django/Vue实现在线考试系统-03-开发环境搭建-MySQL安装
  • python实现波士顿房价预测
  • Pinia不酸,保甜
  • uniapp生命周期
  • 经典卷积模型回顾11—Xception实现图像分类(matlab)
  • 移动App性能测试包含哪些内容?App性能测试工具有哪些?
  • AI测试的迷思
  • [ 红队知识库 ] 一些常用bat文件集合
  • Qt广告机服务器(上位机)
  • SOA架构的理解
  • 如何选择一款数据库?
  • week2
  • JavaScript的学习
  • 用gin写简单的crud后端API接口
  • CF大陆斗C战士(三)
  • TTS | 语音合成论文概述
  • HTML第5天 HTML新标签与特性
  • java ee 之进程
  • Linux学习记录——십사 进程控制(1)
  • 使用 create-react-app 脚手架搭建React项目