js 自动播放的实例代码

下面是关于JS自动播放的实例代码的攻略。

什么是JS自动播放?

JS自动播放通常指网页上的轮播图、视频以及幻灯片等元素自动播放,用户可以通过点击按钮、鼠标移入移出等操作来控制播放、暂停以及跳转。在网页设计中,JS自动播放是一个非常常见的功能。

实现JS自动播放的步骤

步骤一:编写HTML结构

首先需要编写HTML结构,例如:

<div class="slide-container">
  <div class="slide-item"></div>
  <div class="slide-item"></div>
  <div class="slide-item"></div>
</div>

步骤二:编写CSS样式

为了实现滑动的效果,需要对CSS进行设置,例如:

.slide-container {
  width: 800px;
  height: 400px;
  position: relative;
  overflow: hidden;
}
.slide-item {
  width: 800px;
  height: 400px;
  position: absolute;
  top: 0;
  left: 0;
  display: none;
}

步骤三:编写JS代码

  1. 第一种方法:使用setInterval()函数

setInterval()函数用于按照一定的时间间隔周期性地执行一个函数。我们可以使用它来实现自动播放功能。

let currentIndex = 0;
const items = document.querySelectorAll('.slide-item');
const len = items.length;
const interval = 2000; // 每2秒自动播放一次

const slide = () => {
  items[currentIndex].style.display = 'none'; // 隐藏当前元素
  currentIndex = (currentIndex + 1) % len; // 计算下一张要显示的元素的索引
  items[currentIndex].style.display = 'block'; // 显示下一张元素
}

let timer = setInterval(slide, interval); // 开始自动播放

// 当鼠标移动到容器上时,暂停自动播放
document.querySelector('.slide-container').addEventListener('mouseover', () => clearInterval(timer));

// 当鼠标移出容器时,恢复自动播放
document.querySelector('.slide-container').addEventListener('mouseout', () => timer = setInterval(slide, interval));

  1. 第二种方法:使用setTimeout()函数

setTimeout()函数用于在一定的时间之后执行一个函数。我们可以使用它来实现自动播放功能。

let currentIndex = 0;
const items = document.querySelectorAll('.slide-item');
const len = items.length;
const interval = 2000; // 每2秒自动播放一次

const slide = () => {
  items[currentIndex].style.display = 'none'; // 隐藏当前元素
  currentIndex = (currentIndex + 1) % len; // 计算下一张要显示的元素的索引
  items[currentIndex].style.display = 'block'; // 显示下一张元素

  setTimeout(slide, interval); // 继续自动播放
}

slide(); // 开始自动播放

// 当鼠标移动到容器上时,暂停自动播放
document.querySelector('.slide-container').addEventListener('mouseover', () => clearTimeout(timer));

// 当鼠标移出容器时,恢复自动播放
document.querySelector('.slide-container').addEventListener('mouseout', () => slide());

一个示例应用

下面是一个简单的示例,它实现了一个基本的轮播图功能。

HTML代码:

<div class="slide-container">
  <div class="slide-item" style="background-color: red;"></div>
  <div class="slide-item" style="background-color: blue;"></div>
  <div class="slide-item" style="background-color: green;"></div>
  <div class="slide-item" style="background-color: yellow;"></div>
</div>

CSS代码:

.slide-container {
  width: 600px;
  height: 300px;
  position: relative;
  overflow: hidden;
}

.slide-item {
  width: 600px;
  height: 300px;
  position: absolute;
  top: 0;
  left: 0;
  display: none;
}

JS代码:

let currentIndex = 0;
const items = document.querySelectorAll('.slide-item');
const len = items.length;
const interval = 2000;

const slide = () => {
  items[currentIndex].style.display = 'none';
  currentIndex = (currentIndex + 1) % len;
  items[currentIndex].style.display = 'block';
}

let timer = setInterval(slide, interval);

document.querySelector('.slide-container').addEventListener('mouseover', () => clearInterval(timer));
document.querySelector('.slide-container').addEventListener('mouseout', () => timer = setInterval(slide, interval));

另一个示例应用

下面是另一个示例,它实现了一个基本的幻灯片功能。

HTML代码:

<div class="slideshow-container">
  <div class="slideshow-item" style="background-image: url('image1.jpg');"></div>
  <div class="slideshow-item" style="background-image: url('image2.jpg');"></div>
  <div class="slideshow-item" style="background-image: url('image3.jpg');"></div>
</div>

CSS代码:

.slideshow-container {
  width: 600px;
  height: 300px;
  position: relative;
  overflow: hidden;
}

.slideshow-item {
  width: 600px;
  height: 300px;
  position: absolute;
  top: 0;
  left: 0;
  background-position: center;
  background-size: cover;
  opacity: 0;
  transition: opacity 1s ease-in-out;
}

.slideshow-item.active {
  opacity: 1;
}

JS代码:

let currentIndex = 0;
const items = document.querySelectorAll('.slideshow-item');
const len = items.length;
const interval = 2000;

const slideshow = () => {
  items[currentIndex].classList.remove('active');
  currentIndex = (currentIndex + 1) % len;
  items[currentIndex].classList.add('active');
}

let timer = setInterval(slideshow, interval);

在这个示例中,我们使用了CSS的transition属性来实现淡入淡出的效果。同时,我们还使用了classList来动态地添加和移除‘active’类,以改变元素的外观。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:js 自动播放的实例代码 - Python技术站

(0)
上一篇 2023年5月28日
下一篇 2023年5月28日

相关文章

  • jQWidgets jqxForm值属性

    jQWidgets jqxForm值属性 jQWidgets是一个基于jQuery的UI组件库,提供了丰富的UI组件和工具包括表格、日历、下拉单等。jqxForm是QWidgets的组件,用于创建表单。value属性是jqxForm的一个属性,用于获取或设置表单的值。 value属性的基本语法 value属性用于或设置表单的值,其基本语法如下: //获取表单…

    jquery 2023年5月9日
    00
  • jQWidgets jqxDataTable pageSize属性

    以下是关于“jQWidgets jqxDataTable pageSize属性”的完整攻略,包含两个示例说明: 简介 jqxDataTable 控件的 pageSize 属性用定义每页显示的记录数。 整攻 以下是 jqxDataTable 控件 pageSize 属性的完整攻略: 定义 pageSize 属性 jqxDataTable 控件中,可以使用 pa…

    jquery 2023年5月11日
    00
  • 如何在jQuery中检查一个元素是否被隐藏

    在jQuery中,我们可以使用.is(“:hidden”)方法来检查一个元素是否被隐藏。这个方法会返回一个布尔类型的值,true表示元素被隐藏了,false表示元素可见。下面是具体的使用方法: 第一步:选中要检查的元素 我们需要先选中要检查的元素,可以使用jQuery选择器来选中。例如,这里我们要检查一个id为”myDiv”的div元素是否被隐藏: var …

    jquery 2023年5月13日
    00
  • jQWidgets jqxRadioButton宽度属性

    以下是关于 jQWidgets jqxRadioButton 组件中宽度属性的详细攻略。 jQWidgets jqxRadioButton 宽度属性 jQWidgets jqxRadioButton 组件的宽度属性用于设置单选按钮的宽度。 语法“`javascript // 设置宽度属性$(‘#radioButton’).jqxRadioButton({ …

    jquery 2023年5月12日
    00
  • 判断jQuery是否加载完成,没完成继续判断的解决方法

    判断jQuery是否加载完成,是前端开发中经常会遇到的问题。下面提供两种方法来解决这个问题: 方法一:延迟加载 第一种方法是通过延迟加载方式判断jQuery是否加载完成,具体实现方法如下: <!DOCTYPE html> <html> <head> <title>jQuery延迟加载示例</title&g…

    jquery 2023年5月19日
    00
  • 深入解读JavaScript中的Iterator和for-of循环

    深入解读JavaScript中的Iterator和for-of循环 什么是Iterator? Iterator是ES6新增的一种遍历机制,旨在提供一种统一的遍历接口,使得各种数据结构可以被遍历。Iterator的核心是iterator对象,只要对象具有iterator对象,就可以使用for-of循环遍历,或者自定义遍历方法。 Iterator对象是一个包含n…

    jquery 2023年5月27日
    00
  • 如何使用jQuery在无序列表元素中添加一个列表元素

    在jQuery中,我们可以使用.append()函数向元素添加内容。如果我们想要在无序列表元素中添加一个列表元素,我们可以使用.append()函数来实现。以下是两个示例,演示如何使用jQuery无序列表元素中添加一个列表元素: 示例1添加一个列表元素 以下是一个示例,演示如何使用jQuery在无序元素中添加一个列表元素: <!DOCTYPE html…

    jquery 2023年5月9日
    00
  • jQuery 获取除某指定对象外的其他对象 ( :not() 与.not())

    当使用jQuery选择器时,我们常常会遇到需要获取除了某个指定对象以外的所有其他对象的情况。在这种情况下,我们可以使用 :not() 或 .not() 方法来实现。 :not() 选择器 :not() 选择器是用来排除指定元素或元素组的。如果你需要选择除了某些元素以外的其他所有元素,则可以使用 :not() 选择器。 语法格式: $("select…

    jquery 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部