最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實現(xiàn)輪播圖自動切換

 更新時間:2022年07月12日 08:40:53   作者:聰明的加菲貓  
這篇文章主要為大家詳細介紹了js實現(xiàn)輪播圖自動切換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了js實現(xiàn)輪播圖自動切換的具體代碼,供大家參考,具體內(nèi)容如下

先看效果圖

第一種

?//點擊按鈕,序號變化
showIdx++;
if (showIdx == imgArr.length) {
? ? ? ? ? ? ? ? showIdx = 0;
?}
?
//所有盒子左移動
for (let i = 0; i <items.length; i++) {
?   items[i].style.left = parseFloat(items[i].style.left) - loopbox.offsetWidth + "px";
?
?}
//冗余容器從頁面中刪除
?
//當冗余容器從頁面中移除后,為了保證結(jié)構(gòu)想對應,所以呀item中數(shù)組也要把這個容器刪除
let deleteBox = items.shift();
// console.log(items);
deleteBox.remove();
//在用戶看不到的內(nèi)存中,變更【被從這個頁面刪除的元素的位置
deleteBox.style.left = "900px";
wrapper.appendChild(deleteBox);
items.push(deleteBox);
//把容器從小添加至壓面后,容器加載的圖片在當前的下一站
// 第七步 把容器重新添加至頁面后,容器加載的圖片要是當前這張的下一張
if (showIdx == imgArr.length - 1) {
? ?deleteBox.innerHTML = `<img src=${imgArr[0]}>`;
? ?} else {
? ? ? deleteBox.innerHTML = `<img src=${imgArr[showIdx + 1]}>`;
}

第二種,圖片切換,css代碼

html,
body,
ul,
li {
? ? margin: 0;
? ? padding: 0;
}
?
a {
? ? text-decoration: none;
}
?
.loopbox {
? ? width: 1226px;
? ? height: 460px;
? ? background: #030;
? ? position: relative;
? ? overflow: hidden;
}
?
.box {
? ? width: 100%;
? ? height: 100%;
? ? float: left;
? ? transition: all .3s;
? ? position: absolute;
? ? left: 0;
? ? /* overflow: hidden; */
}
.box.notran{
? ? transition: none;
}
?
.box-item {
? ? /* width: 100%; */
? ? width: 1226px;
? ? height: 100%;
? ? float: left;
? ? background: #f1f1f1;
? ? text-align: center;
? ? font-size: 24px;
? ? line-height: 100px;
? ? /* display: none; */
? ? /* transition: all .3s; */
}
?
.box-item img {
? ? width: 100%;
? ? height: 100%;
? ? /* 圖片適應 */
? ? object-fit: cover;
}
?
.arrow {
? ? width: 60px;
? ? line-height: 30px;
? ? background: #f00;
? ? text-align: center;
? ? color: #f1f1f1;
? ? position: absolute;
? ? top: 50%;
? ? left: 10px;
? ? margin-top: -15px;
? ? border-radius: 15px;
}
?
.arrow:hover {
? ? background: #f60;
}
?
.arrow.arrow-right {
? ? left: auto;
? ? right: 10px;
}
?
.page {
? ? position: absolute;
? ? width: 100%;
? ? text-align: center;
? ? bottom: 10px;
}
?
.page li {
? ? display: inline-block;
? ? width: 80px;
? ? height: 8px;
? ? border-radius: 4px;
? ? background: #000;
}
/* .page li:first-child {
? ? background: #f90;
} */
?
.page li:hover {
? ? background: #f60;
}
?
.page li.current {
? ? background: #f90;
}
?
.side-qq {
? ? width: 100px;
? ? height: 100px;
? ? background: #f00;
? ? /* position: fixed; */
? ? position: absolute;
? ? right: 10px;
? ? top: 450px;
}
?
.navbar {
? ? width: 100%;
? ? background: #ccc;
? ? text-align: center;
}
?
.navbar.fixed {
? ? position: fixed;
? ? left: 0;
? ? top: 0;
}
?
.nav {
? ? height: 21px;
}

js

<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>Document</title>
? ? <link rel="stylesheet" href="./css/index.css">
</head>
<body>
?<!-- 1.分析頁面結(jié)構(gòu) -->
?<div class="loopbox">
? ? <div id="box" class="box">
? ? ? ? <div class="box-item curr"><img src="images/1.webp"></div>
? ? ? ? <div class="box-item"><img src="images/2.webp"></div>
? ? ? ? <div class="box-item"><img src="images/3.webp"></div>
? ? ? ? <div class="box-item"><img src="images/4.webp"></div>
? ? </div>
? ? <a class="arrow arrow-left" href="javascript:;">左</a>
? ? <a class="arrow arrow-right" href="javascript:;">右</a>
? ? <ul id="page" class="page">
? ? ? ? <li class="current"></li>
? ? ? ? <li></li>
? ? ? ? <li></li>
? ? ? ? <li></li>
? ? </ul>
</div>
<script>
? // 1.5.初始化頁面,保證所有的圖片先拍成一排
? let items = document.querySelectorAll(".box-item");
? let lis = document.querySelectorAll(".page li");
? let leftBtn=document.querySelector(".arrow-left")
? ? ? ? let box = document.querySelector(".box");
? ? ? ? let loopbox = document.querySelector(".loopbox");
? ? ? ? box.style.width = items.length * loopbox.offsetWidth + "px";
? ? ? ? box.style.left = 0+"px";
? ? ? ? // 2.分析功能入口
? ? ? ? let rightBtn = document.querySelector(".arrow-right");
? ? ? ??
? ? ? ? let showIdx = 0;
? ? ? ? rightBtn.onclick = function (){
? ? ? ? ? ? items[showIdx].classList.remove("curr");
? ? ? ? ? ? lis[showIdx].classList.remove("current");
? ? ? ? ? ? showIdx ++;
? ? ? ? ? ? if(showIdx == 4){
? ? ? ? ? ? ? ? showIdx = 0;
? ? ? ? ? ? }
? ? ? ? ? ? items[showIdx].classList.add("curr");
? ? ? ? ? ? lis[showIdx].classList.add("current");
? ? ? ? ? ? box.style.left = (-1) * showIdx * loopbox.offsetWidth + "px";
? ? ? ? ? ? for(let i=0;i<lis.length;i++){
?
? ? ? ? ? ? ? ? ?lis[i].onclick =function(){
?
? ? ? ? ? ? ? ? items[showIdx].classList.remove("curr");
? ? ? ? ? ? ? ? lis[showIdx].classList.remove("current");
? ? ? ? ? ? ? ? showIdx=i;
? ? ? ? ? ? ? ? items[showIdx].classList.add("curr");
? ? ? ? ? ? ? ? lis[showIdx].classList.add("current");
? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? }
? ? ? ? ? ? leftBtn.onclick = function(){
? ? ? ? //第一張 消失(取消類名)
? ? ? ? ? ?items[showIdx].classList.remove("curr");
? ? ? ? ? ?lis[showIdx].classList.remove("current");
? ? ? ? ? ?showIdx --;
? ? ? ? ? ?//預知判斷
? ? ? ? ? ?if(showIdx == -1){
? ? ? ? ? ? ? ?//點擊之后,點擊之前意味著已經(jīng)在加,需要歸零
? ? ? ? ? ? ? ?showIdx = 3;
? ? ? ? ? ?}
? ? ? ? ? ?items[showIdx].classList.add("curr");
? ? ? ? ? ?lis[showIdx].classList.add("current");
? ? ? ? ? ?box.style.left = (-1) * showIdx * loopbox.offsetWidth + "px";
? ? ? ? // 第二張 出現(xiàn)(添加類名)showIdx+1
? ? ? ? };
? ? ? ? for(let j=0;j<lis.length;j++){
? ? ? ? ? ? lis[j].onclick ?= function(){
? ? ? ? ? ? ? ? items[showIdx].classList.remove("curr");
? ? ? ? ? ? ? ? lis[showIdx].classList.remove("current"); ?
? ? ? ? ? ? ? ? //選好變?yōu)辄c擊序號對應結(jié)構(gòu)
? ? ? ? ? ? ? ? showIdx=j;
? ? ? ? ? ? ? ? items[showIdx].classList.add("curr");
? ? ? ? ? ? ? ? lis[showIdx].classList.add("current");
? ? ? ? ? ? }
? ? ? ??
? ? ? ? ? ? }
?
?
? ? ? ? }
? ? ? ? function time(){
? ? items[showIdx].classList.remove("curr");
? ? ? ? ? ? lis[showIdx].classList.remove("current");
? ? ? ? ? ? showIdx ++;
? ? ? ? ? ? if(showIdx == 4){
? ? ? ? ? ? ? ? showIdx = 0;
? ? ? ? ? ? }
? ? ? ? ? ? items[showIdx].classList.add("curr");
? ? ? ? ? ? lis[showIdx].classList.add("current");
? ? ? ? ? ? box.style.left = (-1) * showIdx * loopbox.offsetWidth + "px";
? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ? for(let i=0;i<lis.length;i++){
?
? ? ? ? ? ? lis[i].onclick =function(){
?
? ? ? ? items[showIdx].classList.remove("curr");
? ? ? ? lis[showIdx].classList.remove("current");
? ? ? ? showIdx=i;
? ? ? ? items[showIdx].classList.add("curr");
? ? ? ? lis[showIdx].classList.add("current");
}
? ? ? ? }
? ? ?setInterval(time,3000) ??
? ??
?
</script>
</body>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • javascript函數(shù)中參數(shù)傳遞問題示例探討

    javascript函數(shù)中參數(shù)傳遞問題示例探討

    本節(jié)主要與大家探討下javascript函數(shù)中參數(shù)傳遞問題,有不明白的朋友可以參考下
    2014-07-07
  • js前端傳json后臺接收‘‘被轉(zhuǎn)為quot的問題解決

    js前端傳json后臺接收‘‘被轉(zhuǎn)為quot的問題解決

    這篇文章主要介紹了js前端傳json后臺接收‘‘被轉(zhuǎn)為&quot;的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • 前端項目部署后如何提示用戶版本更新詳解

    前端項目部署后如何提示用戶版本更新詳解

    這篇文章主要給大家介紹了關(guān)于前端項目部署后如何提示用戶版本更新的相關(guān)資料,文中通過代碼介紹的非常詳細,對大家的工作或者學習具有一定的參考借鑒價值,需要的朋友可以參考下
    2024-03-03
  • uniapp中實現(xiàn)canvas超出屏幕滾動查看功能

    uniapp中實現(xiàn)canvas超出屏幕滾動查看功能

    親愛的小伙伴,當你需要在uniapp中使用canvas繪制一個超長圖,就類似于橫向的流程圖時,這個canvas超出屏幕部分拖動屏幕查看會變得十分棘手,怎么解決這個問題呢,下面小編給大家介紹uniapp中實現(xiàn)canvas超出屏幕滾動查看功能,感興趣的朋友一起看看吧
    2024-03-03
  • JavaScript實現(xiàn)三級聯(lián)動效果

    JavaScript實現(xiàn)三級聯(lián)動效果

    這篇文章主要為大家詳細介紹了JavaScript實現(xiàn)三級聯(lián)動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 用js控制視頻播放進度基本示例代碼

    用js控制視頻播放進度基本示例代碼

    寫前端的時候,很多的時候是需要支持要網(wǎng)頁視頻播放的功能,下面這篇文章主要給大家介紹了關(guān)于用js控制視頻播放進度的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2025-04-04
  • JS input文本框禁用右鍵和復制粘貼功能的代碼

    JS input文本框禁用右鍵和復制粘貼功能的代碼

    由于項目要求,有些文本框需要禁用掉右鍵和復制粘貼的功能,昨天剛剛用JS實現(xiàn)。
    2010-04-04
  • 固定表格行列(expression)在IE下適用

    固定表格行列(expression)在IE下適用

    本文為大家介紹下使用expression固定表格行列,這是一種在IE下適用的固定行列的方法,感興趣的朋友可以學習下,希望對大家有所幫助
    2013-07-07
  • js方塊躲避游戲代碼

    js方塊躲避游戲代碼

    鼠標控制,空色方塊不要讓藍色方塊碰到就可以,看能玩多久,是個javascript不錯的一個游戲啊,想用js寫游戲的朋友可以參考下,看代碼應該是國外的人寫的
    2008-05-05
  • Markdown與Bootstrap相結(jié)合實現(xiàn)圖片自適應屬性

    Markdown與Bootstrap相結(jié)合實現(xiàn)圖片自適應屬性

    Markdown 是一種輕量級的標記語言,它的優(yōu)點很多,目前也被越來越多的寫作愛好者,撰稿者廣泛使用。接下來通過本文給大家介紹Markdown與Bootstrap相結(jié)合實現(xiàn)圖片自適應屬性,感興趣的朋友一起學習吧
    2016-05-05

最新評論

肃宁县| 锦州市| 连山| 会东县| 镇赉县| 化隆| 宜阳县| 安岳县| 余姚市| 浏阳市| 政和县| 新建县| 富平县| 佛坪县| 易门县| 迁安市| 安顺市| 永川市| 攀枝花市| 睢宁县| 封开县| 伊春市| 平陆县| 克山县| 金寨县| 蒲城县| 威海市| 定安县| 大新县| 怀远县| 田阳县| 昌江| 柯坪县| 建昌县| 景洪市| 晋州市| 奉化市| 石景山区| 信丰县| 济宁市| 福海县|