原生js实现缩略图

2024-06-12 01:52
文章标签 实现 js 原生 缩略图

本文主要是介绍原生js实现缩略图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

单张图片的实现代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>* {margin: 0;padding: 0;}.image-container {position: relative;display: inline-block;}.image {display: block;width: 500px;height: auto;}.mask {position: absolute;width: 100px;height: 100px;background-color: rgba(0, 0, 0, .5);cursor: move;opacity: 0;top: 0;left: 0;}.large-image {opacity: 0;position: absolute;width: 200px;height: 200px;background-repeat: no-repeat;z-index: 1001;pointer-events: none;}</style>
</head><body><div class="image-container"><img alt="Image" class="image"><div class="mask"></div><div class="large-image"></div></div><script>let container = document.querySelector('.image-container')let mask = document.querySelector(".mask");let image = document.querySelector(".image");let largeImage = document.querySelector(".large-image");let list = 'https://cdn.pixabay.com/photo/2018/02/08/10/22/desk-3139127_1280.jpg'image.src = listcontainer.onmousemove = function (event) {// 显示遮罩和放大的图片mask.style.opacity = 1;largeImage.style.opacity = 1;// 获取图片的宽高let imageRect = image.getBoundingClientRect();let maskWidth = mask.offsetWidth;let maskHeight = mask.offsetHeight;// 获取鼠标相对于图片的位置let mouseX = event.clientX - imageRect.left;let mouseY = event.clientY - imageRect.top;// 设置遮罩的宽高mask.style.width = imageRect.width / 5 + 'px';mask.style.height = imageRect.width / 5 + 'px';// 设置放大的图片的宽高largeImage.style.width = imageRect.width / 2 + 'px';largeImage.style.height = imageRect.width / 2 + 'px';// 确保鼠标在图片内部if (mouseX < 0 ||mouseY < 0 ||mouseX > imageRect.width ||mouseY > imageRect.height) {mask.style.opacity = 0;largeImage.style.opacity = 0;return;}// 计算遮罩的位置let viewX = Math.max(0, Math.min(imageRect.width - maskWidth, mouseX - maskWidth / 2));let viewY = Math.max(0, Math.min(imageRect.height - maskHeight, mouseY - maskHeight / 2));mask.style.left = viewX + "px";mask.style.top = viewY + "px";// 计算放大图片的位置let zoomFactor = window.devicePixelRatio != 1 ? window.devicePixelRatio : 2; // 调整放大倍数let backgroundPosX = -viewX * zoomFactor;let backgroundPosY = -viewY * zoomFactor;// 设置放大图片的位置largeImage.style.left = event.pageX + 10 + "px";largeImage.style.top = event.pageY + 10 + "px";largeImage.style.backgroundImage = `url('${image.src}')`;largeImage.style.backgroundPosition = `${backgroundPosX}px ${backgroundPosY}px`;largeImage.style.backgroundSize = `${imageRect.width * zoomFactor}px ${imageRect.height * zoomFactor}px`;}container.onmouseleave = function () {// 隐藏遮罩和放大的图片mask.style.opacity = 0;largeImage.style.opacity = 0;};</script>
</body></html>

多张图片的实现代码

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>.container {display: flex;padding: 100px;height: 600px;}.left-dom {border: 1px solid #222;width: 40%;display: flex;flex-direction: column;}.current-img {position: absolute;width: 100%;/* 图片宽度填满父容器 */height: 100%;/* 图片高度填满父容器 */object-fit: cover;/* 让图片按比例缩放填满容器 */}.mask {width: 200px;height: 200px;background-color: rgba(2, 2, 2, 0.4);position: absolute;z-index: 2;opacity: 0;}.top-img-list {flex: 1;position: relative;}.botm-img-list {border-top: 1px solid #222;width: 100%;padding: 10px;box-sizing: border-box;}.right-dom {flex: 1;}.botm-img-list>img {width: 100px;height: 100px;cursor: pointer;margin-right: 20px;border: 2px solid transparent;box-sizing: border-box;}.active {border: 2px solid red !important;}.botm-img-list>img:last-of-type {margin-right: 0px;}.right-dom {position: relative;background-repeat: no-repeat;}</style>
</head><body><div class="container"><div class="left-dom"><div class="top-img-list"><img class="current-img" /><div class="mask"></div></div><div class="botm-img-list"></div></div><div class="right-dom"></div></div><script>const list = ["https://www.2008php.com/2011_Website_appreciate/2011-03-06/20110306201755.jpg","https://www.2008php.com/2011_Website_appreciate/11-03-06/20110306201933.jpg"]const botmDom = document.querySelector('.botm-img-list');const curImg = document.querySelector('.current-img');var imgWidth = null, imgHeight = null;function getImgSize(src) {// 创建一个新的Image对象const img = new Image();// 设置Image对象的src为图片的URLimg.src = src; // 替换成你的图片URLreturn new Promise((resolve, reject) => {// 等待图片加载完成img.onload = function () {resolve({width: this.width,height: this.height})};})}function setSize(src) {const res = getImgSize(src).then(res => {imgWidth = res.width;imgHeight = res.height;})}function clearActive() {const activeImg = document.querySelectorAll('.active')for (let i = 0; i < activeImg.length; i++) {activeImg[i].classList.remove('active');}}list.forEach((v, i) => {const img = document.createElement('img');img.src = v;if (i === 0) img.classList.add('active');botmDom.append(img);img.addEventListener('click', e => {curImg.src = v;clearActive();img.classList.add('active');rightDom.style.backgroundImage = 'url(' + v + ')';setSize(v);})})const topDom = document.querySelector('.top-img-list');const rightDom = document.querySelector('.right-dom');const mask = document.querySelector('.mask');topDom.addEventListener('mouseleave', e => {mask.style.opacity = 0;rightDom.style.opacity = 0;})topDom.addEventListener('mousemove', e => {mask.style.opacity = 1;rightDom.style.opacity = 1;var left = e.clientX - mask.offsetWidth;var top = e.clientY - mask.offsetHeight;const maxHeight = topDom.offsetHeight - mask.offsetHeightconst maxWidth = topDom.offsetWidth - mask.offsetWidthif (left < 0) left = 0else if (left > maxWidth) left = maxWidthmask.style.left = left + 'px';rightDom.style.backgroundPositionX = -left / maxWidth * (imgWidth - rightDom.offsetWidth) + 'px';if (top < 0) top = 0else if (top > maxHeight) top = maxHeightmask.style.top = top + 'px';rightDom.style.backgroundPositionY = -top / maxHeight * (imgHeight - rightDom.offsetHeight) + 'px';})curImg.src = list[0]rightDom.style.backgroundImage = 'url(' + list[0] + ')';setSize(list[0]);</script>
</body></html>

这篇关于原生js实现缩略图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1052865

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4