百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

使用原生js、css和html实现图片画廊组件

zhezhongyun 2024-12-26 17:41 57 浏览

图片画廊组件是网站中常见的UI组件,尤其是在电商平台的产品详情页上,它允许用户通过缩略图快速浏览和查看产品的多个图片。本文介绍如何仅使用原生的js、css和html实现下面动画呈现的图片画廊组件。

功能介绍

  1. html结构中上方为主图区域,下方为缩略图列表,缩略图列表的两边为控制水平左右滑动的箭头导航;
  2. 鼠标移动到某个缩略图上时,主图区域将显示缩略图的大图,并且缩略图着红色边框以突出显示;
  3. 点击右侧箭头,缩略图向左侧滑动直到最右侧的缩略图显示在视野中,此时右侧箭头失效;类似的,点击左侧箭头,缩略图向右侧滑动直到最左侧缩略图出现在视野中,此时左侧箭头失效。

HTML结构

首先创建HTML结构,包括主图区域和下方导航区域,需要重点交代的是id为spec-list的div元素是缩略图列表的容器,容器的position属性是relative,设置了固定的宽度,overflow设置为hidden,这样其子元素超过宽度的部分将不可见,它就相当于窗户,提供了一个矩形的的可见视野。ul装载所有的缩略图,它的position属性设置为absolute,这样就可以基于其父元素设置偏移量,它的宽度大于父元素的宽度,这样就通过设置left属性实现左右滑动,在父窗口范围内的缩略图将是可见的,这样就实现了滑动效果。

<div class="product-intro">
	<div class="preview-wrap">
		<div class="preview" id="preview">
			<!-- 主图显示区域 -->
			<div class="main-img" style="width: 460px; height: 460px;">
				<img id="spec-img" alt="" src="./images/ai-generated-8833166_1280.webp" 
					style="width: 100%; height: 100%; object-fit: cover;">
			</div>

			<!-- 下方导航列表 -->
			<div class="spec-list" style="width: 452px;">
				<!-- 左侧箭头 -->
				<a id="spec-forward" href="javascript:;" class="arrow-prev disabled">
					<i class="sprite-arrow-prev">
						<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 24 24" style="scale:2;">
							<path fill="currentColor" fill-rule="evenodd" d="m15 4l2 2l-6 6l6 6l-2 2l-8-8z"/>
						</svg>
					</i>
				</a>
				<!-- 右侧箭头 -->
				<a id="spec-backward" href="javascript:;" class="arrow-next">
					<i class="sprite-arrow-next">
						<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 24 24" style="scale:2;">
							<path fill="currentColor" fill-rule="evenodd" d="m9.005 4l8 8l-8 8L7 18l6.005-6L7 6z"/>
						</svg>
					</i>
				</a>
				<!-- 缩略图列表可见区域 -->
				<div id="spec-list" class="spec-items"
					style="position: relative; width: 380px; height: 58px; overflow: hidden;">
					<!-- 缩略图列表 -->
					<ul class="lh" style="position: absolute; width: 456px; height: 58px; top: 0px; left: 0px;">
						<li class="img-hover"><img alt="" src="./images/ai-generated-8833166_1280.webp" width="54" height="54"></li>
						<li class=""><img alt="" src="./images/owl-50267_1280.jpg" width="54" height="54"></li>
						<li class=""><img alt="" src="./images/seal-8834240_1280.webp" width="54" height="54"></li>
						<li class=""><img alt="" src="./images/stork-8830107_1280.webp" width="54" height="54"></li>
						<li class=""><img alt="" src="./images/triggerfish-8832563_1280.webp" width="54" height="54"></li>
						<li class=""><img alt="" src="./images/ai-generated-8834126_1280.webp" width="54" height="54"></li>
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>

CSS样式

.product-intro {
    position: relative;
    z-index: 1;
    margin-top: 10px;
    padding-bottom: 10px
}

.product-intro .preview-wrap {
    float: left;
    padding-bottom: 15px;
    position: relative;
    zoom:1;
    z-index: 7
}

.preview {
    position: relative
}

.preview .main-img {
    border: 1px solid #eee;
    margin-bottom: 20px;
    zoom: 1
}

.preview svg {
    color: #CCCCCC;
}

.preview .spec-list {
    margin-bottom: 18px;
    position: relative;
    zoom: 1
}

.preview .spec-list ul {
    margin: 0;
    transition: left 0.5s ease;
    list-style-type: none;
    padding-left: 0;
}

.preview .spec-list .arrow-next,.preview .spec-list .arrow-prev {
    display: block;
    width: 22px;
    height: 32px;
    float: left;
    position: absolute;
    cursor: pointer;
    top: 50%;
    margin-top: -16px
}

.preview .spec-list .arrow-next i,.preview .spec-list .arrow-prev i {
    display: block
}

.preview .spec-list .arrow-prev {
    left: 0
}

.preview .spec-list .arrow-prev:hover i svg {
    color: #999999;
}

.preview .spec-list .arrow-prev.disabled i svg {
    color: #DFDFDF;
}

.preview .spec-list .arrow-next {
    right: 0
}

.preview .spec-list .arrow-next:hover i svg {
    color: #999999;
}

.preview .spec-list .arrow-next.disabled i svg {
    color: #DFDFDF;
}

.preview .spec-items {
    width: 224px;
    margin: 0 auto;
    overflow: hidden
}

.preview .spec-items ul {
    width: 2000px
}

.preview .spec-items ul li {
    float: left;
    margin: 0 9px;
    max-width: 60px;
    max-height: 70px
}

.preview .spec-items ul li img {
    border: 2px solid #fff;
    padding-bottom: 1px
}

.preview .spec-items ul li.img-hover img,.preview .spec-items ul li:hover img {
    border: 2px solid #e53e41
}

.preview #spec-img {
    max-height: 600px;
}

.preview .spec-list .spec-items {
    width: 390px
}

JavaScript交互

js主要处理鼠标hover到缩略图更新主图区域图片的src属性值,以及缩略图的红色边框效果;以及实现左右侧箭头点击产生的缩略图列表左右滑动效果、箭头失效处理,注意js中是直接设置ul的left属性值,要实现滑动的动画效果,需要在css样式中设置transition属性为left 0.5s ease,否则就不会产生动画效果。

(function() {
    // 监听缩略图的鼠标进入事件,更新显示大图
    var thumbnail_img_list = document.querySelectorAll("#spec-list ul li img");
    var spec_img = document.getElementById("spec-img");
    for (let i=0; i < thumbnail_img_list.length; i++) {
        let thumbnail_img = thumbnail_img_list[i];
        thumbnail_img.addEventListener("mouseenter", function(e) {
            // 移除当前img-hover类
            let img_pre_hoverd = document.querySelector("#spec-list ul li.img-hover");
            if (img_pre_hoverd) {
                img_pre_hoverd.classList.remove("img-hover");
            }
            // 添加当前img-hover类
            e.target.parentNode.classList.add("img-hover");
            // 更新主图区域的图片源
            spec_img.src = thumbnail_img.src;
        });
    }

    // 缩略图的左右滑动效果
    var btn_next = document.getElementById("spec-backward");
    var btn_pre = document.getElementById("spec-forward");
    var spec_list = document.querySelector("#spec-list");
    var spec_list_ul = document.querySelector("#spec-list ul");
    
    // 监听右侧箭头的点击事件
    btn_next.addEventListener("click", function(e) {
        if (!this.classList.contains("disabled")) {
            // 点击右侧箭头,缩略图左侧滑动直到最右侧缩略图可见,即left为缩略图宽度-可见容器宽度,为负值
            spec_list_ul.style.left = (spec_list.clientWidth - spec_list_ul.clientWidth) + "px";

            // 右侧箭头失效
            this.classList.add("disabled");
            // 左侧箭头生效
            btn_pre.classList.remove("disabled");
        }
    });

    // 点击左侧箭头的点击事件
    btn_pre.addEventListener("click", function(e) {
        if (!this.classList.contains("disabled")) {
            // 点击左侧箭头,缩略图右侧滑动直到最左侧缩略图可见,即left为0
            spec_list_ul.style.left = "0px";

            // 左侧箭头失效
            this.classList.add("disabled");
            // 右侧箭头失效
            btn_next.classList.remove("disabled");
        }
    });

})();

相关推荐

perl基础——循环控制_principle循环

在编程中,我们往往需要进行不同情况的判断,选择,重复操作。这些时候我们需要对简单语句来添加循环控制变量或者命令。if/unless我们需要在满足特定条件下再执行的语句,可以通过if/unle...

CHAPTER 2 The Antechamber of M de Treville 第二章 特雷维尔先生的前厅

CHAPTER1TheThreePresentsofD'ArtagnantheElderCHAPTER2TheAntechamber...

CHAPTER 5 The King&#39;S Musketeers and the Cardinal&#39;S Guards 第五章 国王的火枪手和红衣主教的卫士

CHAPTER3TheAudienceCHAPTER5TheKing'SMusketeersandtheCardinal'SGuard...

CHAPTER 3 The Audience 第三章 接见

CHAPTER3TheAudienceCHAPTER3TheAudience第三章接见M.DeTrévillewasatt...

别搞印象流!数据说明谁才是外线防守第一人!

来源:Reddit译者:@assholeeric编辑:伯伦WhoarethebestperimeterdefendersintheNBA?Here'sagraphofStea...

V-Day commemorations prove anti-China claims hollow

People'sLiberationArmyhonorguardstakepartinthemilitaryparademarkingthe80thanniversary...

EasyPoi使用_easypoi api

EasyPoi的主要特点:1.设计精巧,使用简单2.接口丰富,扩展简单3.默认值多,writelessdomore4.springmvc支持,web导出可以简单明了使用1.easypoi...

关于Oracle数据库12c 新特性总结_oracle数据库12514

概述今天主要简单介绍一下Oracle12c的一些新特性,仅供参考。参考:http://docs.oracle.com/database/121/NEWFT/chapter12102.htm#NEWFT...

【开发者成长】JAVA 线上故障排查完整套路!

线上故障主要会包括CPU、磁盘、内存以及网络问题,而大多数故障可能会包含不止一个层面的问题,所以进行排查时候尽量四个方面依次排查一遍。同时例如jstack、jmap等工具也是不囿于一个方面的问题...

使用 Python 向多个地址发送电子邮件

在本文中,我们将演示如何使用Python编程语言向使用不同电子邮件地址的不同收件人发送电子邮件。具体来说,我们将向许多不同的人发送电子邮件。使用Python向多个地址发送电子邮件Python...

提高工作效率的--Linux常用命令,能够决解95%以上的问题

点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf为什么要学习Linux命令?1、因为Li...

linux常用系统命令_linux操作系统常用命令

系统信息arch显示机器的处理器架构dmidecode-q显示硬件系统部件-(SMBIOS/DMI)hdparm-i/dev/hda罗列一个磁盘的架构特性hdparm-tT/dev/s...

小白入门必知必会-PostgreSQL-15.2源码编译安装

一PostgreSQL编译安装1.1下载源码包在PostgreSQL官方主页https://www.postgresql.org/ftp/source/下载区选择所需格式的源码包下载。cd/we...

Linux操作系统之常用命令_linux系统常用命令详解

Linux操作系统一、常用命令1.系统(1)系统信息arch显示机器的处理器架构uname-m显示机器的处理器架构uname-r显示正在使用的内核版本dmidecode-q显示硬件系...

linux网络命名空间简介_linux 网络相关命令

此篇会以例子的方式介绍下linux网络命名空间。此例中会创建两个networknamespace:nsa、nsb,一个网桥bridge0,nsa、nsb中添加网络设备veth,网络设备间...