DOM操作和事件监听综合练习 (具备三种功能的轮播图案例)

目录

[练习1 定时轮播图](#练习1 定时轮播图)

[练习2 按钮轮播](#练习2 按钮轮播)

[练习3 键盘轮播图](#练习3 键盘轮播图)


练习1 定时轮播图

运行效果如下:

图片自动切换(轮播图效果)

参考代码:

html 复制代码
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>图片自动切换(轮播图效果)</title>  
    <style>  
        body, html {  
            margin: 0;  
            padding: 0; 
            width: 100%; 
            height: 100%;  
        }  
        .carousel-container {  
            position: relative;  
            width: 25%;  
            height: 40%; /* 根据需要设置高度 */ 
            border: 4px red solid;
            background-color: gray;
        }  
        .carousel-container .carousel-image {  
            position: absolute;  
            top: 0;  
            left: 0;  
            width: 100%;  
            height: 100%;  
            opacity: 1; /* 不透明度0-1 */  
        }  

        .change-image{
            width: 15%;  
            height: 3%;  
            /* border: 1px purple solid; */
            position: absolute;
            top: 30%;
            left: 5%;
        }

        .change-image .button{        
            width: 50px;  
            height: 30px;
            color: white;
            text-align: center;
            background-color: red;
            border-radius: 10px;
            margin-left: 9px;
            float: left;
        }



    </style>  
</head>  
<body>  

    <div class="carousel-container">  
        <img src="./图片/p1.jpg" class="carousel-image" id="img1">  

    </div> 

    <script>  
        const imgElement = document.getElementById("img1"); 
        const divElement = document.getElementsByClassName("carousel-container")[0];
        var i = 1;

            
        // 修改img标签的图片路径
        function showNextImage1() {     

            if(i>4){
                i = 1;
            }else{               
                imgElement.src = `./图片/p${i}.jpg`;
                i =  i + 1;
            } 

        }  
        //   方案二:直接改整个父节点下节点(innerHTML实现)
        // function showNextImage2() {           
        //     if(i>4){
        //         i = 1;
        //     }else{     
        //         divElement.innerHTML = `<img src="./img_src/p${i}.jpg" class="carousel-image active" id="img1">`
        //         i =  i + 1;
        //     }     
        // } 

  
        // 每1秒切换一次图片 (无限循环)
        setInterval(showNextImage1, 1500);  


        const p = document.getElementsByTagName("p");

        p[0].addEventListener("click",
            function(){
                i = 1;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )

        p[1].addEventListener("click",
            function(){
                i = 2;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )

        p[2].addEventListener("click",
            function(){
                i = 3;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )

        p[3].addEventListener("click",
            function(){
                i = 4;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )

    </script>  
</body>  
</html>

练习2 按钮轮播

运行效果如下:

图片自动切换,按钮切换

参考代码:

html 复制代码
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>图片自动切换(轮播图效果)</title>  
    <style>  
    /* z整体 */
        body, html {  
            margin: 0;  
            padding: 0; 
            width: 100%; 
            height: 100%;  
        } 

 
        .carousel-container {  
            position: relative;  
            width: 25%;  
            height: 40%; /* 根据需要设置高度 */ 
            border: 4px red solid;
            background-color: gray;
        }  

        .carousel-container .carousel-image {  
            position: absolute;  
            top: 0;  
            left: 0;  
            width: 100%;  
            height: 100%;  
            opacity: 1; /* 不透明度0-1 */  
        } 
        .change-image{
            width: 20%;  
            height: 3%;  
            /* border: 1px purple solid; */
            position: absolute;
            top: 30%;
            left: 5%;
        }
        .change-image .button{        
            width: 50px;  
            height: 30px;
            color: white;
            text-align: center;
            background-color: red;
            border-radius: 10px;
            margin-left: 9px;
            float: left;
        }
   

        .clear_ele::after{
            content: "";  /* 这个伪元素的内容属性必须有 */
            display: block;
            /* border: 6px purple dashed; */
            clear: both;
        }

    </style>  
</head>  
<body>  

    <div class="carousel-container">  
        <img src="./图片/p1.jpg" class="carousel-image" id="img1">  

    </div> 

    <div  class="clear_ele change-image">
        <p class="button"  id="p1">第1张</p>
        <p class="button"  id="p2">第2张</p>
        <p class="button"  id="p3">第3张</p>
        <p class="button"  id="p4">第4张</p>   
    </div>

    <script>  

  
        const imgElement = document.getElementById("img1"); 
        const divElement = document.getElementsByClassName("carousel-container")[0];
        var i = 1;

            
        // 修改img标签的图片路径
        function showNextImage1() {     

            if(i>4){
                i = 1;
            }else{               
                imgElement.src = `./图片/p${i}.jpg`;
                i =  i + 1;
            } 

        }  
        //   方案二:直接改整个父节点下节点(innerHTML实现)
        // function showNextImage2() {           
        //     if(i>4){
        //         i = 1;
        //     }else{     
        //         divElement.innerHTML = `<img src="./img_src/p${i}.jpg" class="carousel-image active" id="img1">`
        //         i =  i + 1;
        //     }     
        // } 

  
        // 每1秒切换一次图片 (无限循环)
        setInterval(showNextImage1, 1500);  


        const p = document.getElementsByTagName("p");

        p[0].addEventListener("click",
            function(){
                i = 1;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )

        p[1].addEventListener("click",
            function(){
                i = 2;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )

        p[2].addEventListener("click",
            function(){
                i = 3;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )

        p[3].addEventListener("click",
            function(){
                i = 4;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )


        const outputDiv = document.getElementById('output');  
        ocument.addEventListener(
            
            'keydown',

            function(event) {  
                
                const keyCode = event.key; 

                const message = `键盘 ${keyCode}  被按下`; 
                outputDiv.textContent += message + '\n';  
                outputDiv.scrollTop = outputDiv.scrollHeight;  

}
);  


box.addEventListener( "button",
            function(){
                console.log("鼠标双击事件已触发");
            }
        );
        
    </script>  
</body>  
</html>

练习3 键盘轮播图

运行效果:

按下 Enter键 图片开始自动轮播,按钮变为红色。

再次按下Enter键,图片停止轮播按钮变回绿色。如图:

参考代码:

html 复制代码
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>图片自动切换(轮播图效果)</title>  
    <style>  
        body, html {  
            margin: 0;  
            padding: 0; 
            width: 100%; 
            height: 100%;  
        } 

        .carousel-container {  
            position: relative;  
            width: 25%;  
            height: 40%; /* 根据需要设置高度 */ 
            border: 4px red solid;
            background-color: gray;
        }  

        .carousel-container .carousel-image {  
            position: absolute;  
            top: 0;  
            left: 0;  
            width: 100%;  
            height: 100%;  
            opacity: 1; /* 不透明度0-1 */  
        } 
        .change-image{
            width: 20%;  
            height: 3%;  
            /* border: 1px purple solid; */
            position: absolute;
            top: 30%;
            left: 5%;
        }
     .change-image .button{        
            width: 50px;  
            height: 30px;
            color: white;
            text-align: center;
            background-color: rgb(255, 0, 0);
            border-radius: 10px;
            margin-left: 9px;
            float: left;

        }
   

   
        .clear_ele::after{
            content: "";  /* 这个伪元素的内容属性必须有 */
            display: block;
            /* border: 6px purple dashed; */
            clear: both;
        }
  
        #output {  
            color: white;
            background-color: rgb(0, 133, 60); 
            text-align: center;
            width: 15%;  
            height: 3%; 
            margin-top: 1%;      
            position: relative;
            left: 5%; 
            border-radius: 10px;
          
        } 
    </style>  
</head>  
<body>  

    <div class="carousel-container">  
        <img src="./图片/p1.jpg" class="carousel-image" id="img1">  

    </div> 

    <div  class="clear_ele change-image">
        <p class="button"  id="p1">第1张</p>
        <p class="button"  id="p2">第2张</p>
        <p class="button"  id="p3">第3张</p>
        <p class="button"  id="p4">第4张</p>   
    </div>
    <div id="output">图片轮播关闭......(按Enter键开启轮播)</div>
    <script>  

  
        const imgElement = document.getElementById("img1"); 
        var i = 1;
        var scroll_img = false;
        function showNextImage1() {     
            if(scroll_img){
                if(i>4){
                    i = 1;
                }else{               
                    imgElement.src = `./图片/p1.jpg${i}.jpg`;
                    i =  i + 1;
                } 
            }
        }  
  
        // 每1秒切换一次图片 (无限循环)
        setInterval(showNextImage1, 1500);  


        const p = document.getElementsByTagName("p");

        p[0].addEventListener("click",
            function(){
                i = 1;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )

        p[1].addEventListener("click",
            function(){
                i = 2;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )

        p[2].addEventListener("click",
            function(){
                i = 3;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )

        p[3].addEventListener("click",
            function(){
                i = 4;
                imgElement.src = `./图片/p${i}.jpg`;
            }
        )




    // 【实现回车键控制轮播是否开启】    
      
    const outputDiv = document.getElementById('output');  // 获取显示按键信息的元素
      
    document.addEventListener('keydown',  // 监听整个文档的keydown事件
        function(event) {              
            const keyCode = event.key; // 获取按键的代码(包括数字键和特殊键,如箭头键、功能键等)
            if(keyCode==="Enter"){
                scroll_img = !scroll_img;      
            }
            //将提示信息添加到输出区域  
            if (scroll_img) {
                outputDiv.textContent = "图片轮播开启......(按Enter键关闭轮播)";
                outputDiv.style.backgroundColor = "green";
            } else {
                outputDiv.textContent = "图片轮播关闭......(按Enter键开启轮播)";
                outputDiv.style.backgroundColor = "red";
            }
        }
    );


    </script>  
</body>  
</html>
相关推荐
前端初见8 分钟前
彻底搞懂前端环境变量使用和原理
前端
小王码农记20 分钟前
vue中路由缓存
前端·vue.js·缓存·typescript·anti-design-vue
大G哥28 分钟前
我用豆包MarsCode IDE 做了一个 CSS 权重小组件
前端·css
乐闻x32 分钟前
Vue实践篇:如何在 Vue 项目中检测元素是否展示
前端·javascript·vue.js
麻花20131 小时前
WPF里面的C1FlexGrid表格控件添加RadioButton单选
java·服务器·前端
理想不理想v1 小时前
【经典】webpack和vite的区别?
java·前端·javascript·vue.js·面试
羊子雄起1 小时前
CKEditor前端样式和编辑器的样式不一致的问题
前端·编辑器
聊无生1 小时前
JavaSrcipt 函数高级
开发语言·前端·javascript
xiyusec2 小时前
HTML基础
前端·html
好开心332 小时前
javaScript交互案例2
开发语言·前端·javascript·html·ecmascript·交互