图片切换示例2【JavaScript】

devtools/2024/9/24 20:07:57/

这段代码实现了一个简单的图片切换效果。当用户将鼠标悬停在不同的小缩略图上时,主显示框(#box)的背景图片会切换为相应的缩略图所代表的图片。

实现效果:

代码: 

<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>图片切换示例2</title><style>* {padding: 0;margin: 0;}#box {border: 1px solid #ccc;width: 400px;height: 400px;background: url('./image/image1.jpg') no-repeat center center;background-size: cover;}img {width: 30px;height: 30px;}#box ul {list-style: none;}#box ul li {display: inline-block;margin-right: 10px;cursor: pointer;/* 添加光标样式 */}</style></head><body><div id="box"><ul><li data-img="url('./image/image1.jpg')"><img src="./image/image1.jpg" alt="图片1"></li><li data-img="url('./image/image2.jpg')"><img src="./image/image2.jpg" alt="图片2"></li><li data-img="url('./image/image3.jpg')"><img src="./image/image3.jpg" alt="图片3"></li><li data-img="url('./image/image4.jpg')"><img src="./image/image4.jpg" alt="图片4"></li><li data-img="url('./image/image5.jpg')"><img src="./image/image5.jpg" alt="图片5"></li></ul></div><script>document.querySelectorAll('#box ul li').forEach(li => {li.addEventListener('mouseover', function() {const imgSrc = this.getAttribute('data-img');document.getElementById('box').style.background = `${imgSrc} no-repeat center center`;document.getElementById('box').style.backgroundSize = 'cover';});});</script></body>
</html>

部分代码解析:

1. data-* 属性
javascript">data-img="url('./image/image1.jpg')"

使用 data-* 属性:将图片路径存储在 data-img 属性中,这样可以减少对每个 li 的 JavaScript 调用,提高可读性和可维护性。

2. js代码解析
javascript">document.querySelectorAll('#box ul li').forEach(li => {li.addEventListener('mouseover', function() {const imgSrc = this.getAttribute('data-img');document.getElementById('box').style.background = `${imgSrc} no-repeat center center`;document.getElementById('box').style.backgroundSize = 'cover';});});
2.1 选择元素:
javascript">document.querySelectorAll('#box ul li')

这行代码选取了所有在 #box 元素下的 ul 列表中的 li 项。

2.2 遍历每个 li 元素:
javascript">.forEach(li => {

对选中的每个 li 元素执行后面的函数。

2.3 添加事件监听:
javascript">li.addEventListener('mouseover', function() {

为每个 li 元素添加一个鼠标悬停(mouseover)事件的监听器。当鼠标悬停在某个 li 元素上时,执行这个函数。

2.4 获取图片源:
javascript">const imgSrc = this.getAttribute('data-img');

在事件处理函数中,使用 this 关键字指向当前悬停的 li 元素,并获取它的 data-img 属性值,赋值给 imgSrc

2.5 改变背景:
javascript">document.getElementById('box').style.background = `${imgSrc} no-repeat center center`;
document.getElementById('box').style.backgroundSize = 'cover';

将 #box 元素的背景设置为 imgSrc 指定的图片,设置为不重复(no-repeat),居中(center center),并且通过 backgroundSize 设置为覆盖(cover)。


http://www.ppmy.cn/devtools/116648.html

相关文章

project generator 简单使用(二)之 CLion 与 AC6

文章目录 1 AC6 之于 CLion2 配置 progen3 可执行文件 size 显示优化4 测试 1 AC6 之于 CLion 1&#xff09;在上一篇文章中&#xff0c;我们知道 project generator 通过其 “Write Once, Compile any Tool” &#xff08;跨工具&#xff09;的特性&#xff0c;可以让我们使用…

Vue3:$refs和$parent实现组件通信

在Vue3中&#xff0c;refs和refs和parent是用于组件间通信的重要机制 一.$refs 1.操作子组件数据 一旦获取到子组件的实例&#xff0c;父组件可以修改子组件暴露的变量值&#xff0c;实现父子组件间的直接数据交换。 2.批量处理子组件 $refs可以用于同时获取多个子组件的…

Vue相关

Vue2 组件传递事件&#xff1a; props $emit sync v-model $parent / $children $parent获取父组件的实例&#xff0c;任意调用父组件的方法&#xff0c;修改父组件的数据 ref 父组件获取 子组件 实例&#xff0c;任意调用子组件的方法获取子组件的属性 provide / injectp…

【Linux】环境变量

一、引入环境变量 1.1 引入环境变量 main函数是有参数的&#xff0c;该参数就用来接收命令行参数的。当我们在执行文件的过程中&#xff0c;我们可以输入一些选项。 1.1.1 参数说明 argc&#xff08;argument count&#xff09;&#xff1a;表示命令行参数的数量&#xff0c…

c++ pair

C中的pair是一个模板类&#xff0c;用来存储两个不同类型的对象。它位于<utility>头文件中&#xff0c;并定义在std命名空间中。 pair的定义如下&#xff1a; template <class T1, class T2> struct pair {using first_type T1;using second_type T2;T1 first;…

单片机学到什么程度才可以去工作?

说实话&#xff0c;10几年前&#xff0c;我自学单片机转行的时候&#xff0c;也是一头雾水&#xff0c;也是一边苦苦挣扎&#xff0c;一边迷茫的状态。 硬件、软件、编程...样样都需要学&#xff0c;连从哪儿开始都不知道&#xff0c;每次看到那些密密麻麻的电路图和代码&#…

c语言中“函数指针”

变量有地址&#xff0c;数组有地址&#xff0c;那么函数是否有地址呢&#xff1f; 有 int Add(int x,int y){ return xy; } int main() { pritnf("%p\n",&Add); pritnf("%p\n",Add); //&函数名和函数名都是函数的地址&#xff0c;没…

解决 Ubuntu 18.04 下与 TIFFField 和 Anaconda 冲突导致的库加载问题

在 Ubuntu 18.04 系统上&#xff0c;我在安装完ROS后&#xff0c;启动具有 GUI 的软件&#xff08;如 RViz 和 Gazebo&#xff09;时遇到了一个问题&#xff1a;每次尝试启动这些软件时&#xff0c;终端中都会报错&#xff0c;错误信息类似如下&#xff1a; rviz: relocation …