旋转木马轮播图 html+css+js

先看效果(完整代码在底部):

旋转木马轮播图源码与制作过程 html+css+js

能轮播,点击按钮能切图。鼠标位于图片或按钮上停止轮播。

实现(可跟着一步一步实现):

1. 定义标签(video是背景视频,.but1与.but2是左右两个按钮,main与.haha是底层盒子,.box就是旋转的8张图):

<video src="video/3.mp4" autoplay loop muted></video>
    <main id="main">
        <div class="but1"></div>
        <div class="but2"></div>
        <div class="haha">     
        <div  class="box" style="--d: 0;"> <img src="img/1.jpg"> </div>
        <div  class="box" style="--d: 1;"> <img src="img/25.jpg"> </div> 
        <div  class="box" style="--d: 2;"> <img src="img/3.jpg"> </div>
        <div  class="box" style="--d: 3;"> <img src="img/52.jpg"> </div>
        <div  class="box" style="--d: 4;"> <img src="img/6.jpg"> </div>
        <div  class="box" style="--d: 5;"> <img src="img/snow.jpg"> </div>
        <div  class="box" style="--d: 6;"> <img src="img/8.jpg"> </div>
        <div  class="box" style="--d: 7;"> <img src="img/sea.jpg"> </div>
    </div>
    </main>

- -d是var函数的使用,var函数的可以去看这个文章的开头部分
2. 定义底层盒子基本样式:

main{
            position: relative;
            width: 220px;
            height: 130px;
            perspective: 800px;
        }
        .haha{
            position: relative;
            width: 100%;
            height: 100%;
            transform-style: preserve-3d;
            transition: all 1s;
        }    

position: relative;相对定位。
perspective: 800px;改变3D元素是怎样查看透视图。
transform-style: preserve-3d;子元素将保留其 3D 位置。
transition: all 1s; 过渡,后面切图时的时间。

3. 定义按钮基本样式:

.but1,.but2{
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            width: 50px;
            height: 50px;
            font-size: 30px;
            color: white;
            opacity: 0.8;
            line-height: 50px;
            text-align: center;
            border-radius: 50%;
            cursor: pointer;
            user-select: none;
        }
        .but2{        
            left: -320px;           
        }
        .but1{ 
            right: -320px;
        }

transform:translateY:偏移。
opacity:0.8;透明度。
text-align: center; 文本居中。
cursor: pointer; 鼠标外观样式。
user-select: none; 文本不可选。

4. 经过与点击按钮样式变化:

 .but1:hover,.but2:hover{
            box-shadow: inset 0 0 5px rgb(18, 208, 221);
            opacity: 1;
        }
        .but1:active,.but2:active{
           /*  background-color: rgb(22, 163, 81); */
           box-shadow: inset 0 0 5px rgb(18, 208, 221),
           inset 0 0 10px rgb(18, 208, 221),
           inset 0 0 15px rgb(18, 208, 221);
        }

box-shadow:阴影。
opacity: 1; 透明度。

5. 图片的基本样式:

 .box{
            position: absolute;
            width: 100%;
            height: 100%;
            transform: rotateY(calc(var(--d) * 45deg)) translateZ(295px); 
        }
        .box img{
            width: 100%;
            height: 100%;                    
        }

transform: rotateY(calc(var(–d) * 45deg)) translateZ(295px); 每张图旋转与偏移相应的位置得到旋转木马的立体效果。可自己调节~

6. 开始js部分,获取元素与定义变量:

 var haha = document.querySelector(".haha");
        var but1 = document.querySelector(".but1");
        var but2 = document.querySelector(".but2");
        /* 旋转角度 */
        var zhuan = 0;
        /* 设置轮播定时器 */
        var  lunbo =  setInterval(fn,3000);

7. 点击向左按钮事件,点一次转 -45deg:

but1.addEventListener('click',function(){
            zhuan = zhuan - 45;
            haha.style.cssText = ` transform: rotateY(${zhuan}deg); `;
        })

8. 点击向右按钮事件,点一次转 45deg:

but2.addEventListener('click',fn);
 function fn(){
            zhuan = zhuan + 45;
            haha.style.cssText = ` transform: rotateY(${zhuan}deg); `;
        }

9. 鼠标经过按钮与图片时停止轮播定时器,离开图片与按钮开始轮播定时器(简单粗暴):

 but1.addEventListener('mouseover',function(){
            clearInterval(lunbo);
        });
        but1.addEventListener('mouseout',function(){
            lunbo =  setInterval(fn,3000);
        });
         but2.addEventListener('mouseover',function(){
            clearInterval(lunbo);
        });
        but2.addEventListener('mouseout',function(){
            lunbo =  setInterval(fn,3000);
        });
 haha.addEventListener('mouseover',function(){
            clearInterval(lunbo);
        });
       haha.addEventListener('mouseout',function(){
            lunbo =  setInterval(fn,3000);
        });

完整代码:

<!DOCTYPE html>
<html lang="zh-CN">
<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>
    <style>
        @font-face {
  font-family: 'icomoon';
  src:  url('fonts/icomoon.eot?wr5es');
  src:  url('fonts/icomoon.eot?wr5es#iefix') format('embedded-opentype'),
    url('fonts/icomoon.ttf?wr5es') format('truetype'),
    url('fonts/icomoon.woff?wr5es') format('woff'),
    url('fonts/icomoon.svg?wr5es#icomoon') format('svg');
  font-weight: normal;
  font-style: normal;
  font-display: block;
}
        *{
            font-family: 'icomoon';
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        video{
            position: fixed;
            width: 100%;
            height: 100%;
            z-index: -10;
            object-fit: cover;
        }
        body{
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: rgb(7, 14, 34);
        }
        main{
            position: relative;
            width: 220px;
            height: 130px;
            perspective: 800px;
        }
        .haha{
            position: relative;
            width: 100%;
            height: 100%;
            transform-style: preserve-3d;
            transition: all 1s;
        }    
        .but1,.but2{
            position: absolute;
            top: 50%;
            transform: translateY(-50%);
            width: 50px;
            height: 50px;
            font-size: 30px;
            color: white;
            opacity: 0.8;
            line-height: 50px;
            text-align: center;
            border-radius: 50%;
            cursor: pointer;
            user-select: none;
        }
        .but2{        
            left: -320px;           
        }
        .but1{ 
            right: -320px;
        }
        .but1:hover,.but2:hover{
            box-shadow: inset 0 0 5px rgb(18, 208, 221);
            opacity: 1;
        }
        .but1:active,.but2:active{
           /*  background-color: rgb(22, 163, 81); */
           box-shadow: inset 0 0 5px rgb(18, 208, 221),
           inset 0 0 10px rgb(18, 208, 221),
           inset 0 0 15px rgb(18, 208, 221);
        }
        .box{
            position: absolute;
            width: 100%;
            height: 100%;
            transform: rotateY(calc(var(--d) * 45deg)) translateZ(295px); 
        }
        .box img{
            width: 100%;
            height: 100%;                    
        }
        
    </style>
</head>
<body>
    <video src="video/3.mp4" autoplay loop muted></video>
    <main id="main">
        <div class="but1"></div>
        <div class="but2"></div>
        <div class="haha">
     
        <div  class="box" style="--d: 0;"> <img src="img/1.jpg"> </div>
        <div  class="box" style="--d: 1;"> <img src="img/25.jpg"> </div> 
        <div  class="box" style="--d: 2;"> <img src="img/3.jpg"> </div>
        <div  class="box" style="--d: 3;"> <img src="img/52.jpg"> </div>
        <div  class="box" style="--d: 4;"> <img src="img/6.jpg"> </div>
        <div  class="box" style="--d: 5;"> <img src="img/snow.jpg"> </div>
        <div  class="box" style="--d: 6;"> <img src="img/8.jpg"> </div>
        <div  class="box" style="--d: 7;"> <img src="img/sea.jpg"> </div>
    </div>
    </main>

    <script>
        var haha = document.querySelector(".haha");
        var but1 = document.querySelector(".but1");
        var but2 = document.querySelector(".but2");
        /* 旋转角度 */
        var zhuan = 0;
        /* 设置轮播定时器 */
        var  lunbo =  setInterval(fn,3000);
    
         but1.addEventListener('click',function(){
            zhuan = zhuan - 45;
            haha.style.cssText = ` transform: rotateY(${zhuan}deg); `;
        })
        but1.addEventListener('mouseover',function(){
            clearInterval(lunbo);
        });
        but1.addEventListener('mouseout',function(){
            lunbo =  setInterval(fn,3000);
        });
        
        but2.addEventListener('click',fn);
        but2.addEventListener('mouseover',function(){
            clearInterval(lunbo);
        });
        but2.addEventListener('mouseout',function(){
            lunbo =  setInterval(fn,3000);
        });

        function fn(){
            zhuan = zhuan + 45;
            haha.style.cssText = ` transform: rotateY(${zhuan}deg); `;
        }

       haha.addEventListener('mouseover',function(){
            clearInterval(lunbo);
        });
       haha.addEventListener('mouseout',function(){
            lunbo =  setInterval(fn,3000);
        });
   
    </script>

</body>
</html>

总结:

热烈宣布(敲锣打鼓,手舞足蹈),我的B站账号地址:https://space.bilibili.com/176586698

~泪目( Ĭ ^ Ĭ )~

其它文章:
炫彩流光文字 html+css
气泡浮动背景特效 html+css
简约时钟特效 html+css+js
赛博朋克风格按钮 html+css
仿网易云官网轮播图 html+css+js
水波加载动画 html+css
导航栏滚动渐变效果 html+css+js
书本翻页 html+css
3D立体相册 html+css
霓虹灯绘画板效果 html+css+js
记一些css属性总结(一)
Sass总结笔记
…等
在这里插入图片描述

夏天的风,我永远记得。

  • 29
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论
旋转木马轮播图是一种常见的网页设计效果,可以通过HTMLCSS和JavaScript来实现。根据提供的引用内容,可以看出旋转木马轮播图的实现主要是通过点击向左和向右按钮来改变旋转角度,从而实现图片的切换效果。 具体实现步骤如下: 1. 首先,在HTML中创建一个包含图片的容器,可以使用`<div>`元素,并为其设置一个唯一的ID,比如`haha`。 2. 使用CSS来设置容器的样式,包括宽度、高度、位置等,以及设置容器内图片的样式,比如设置宽度、高度、边框等。 3. 在JavaScript中,通过获取向左和向右按钮的元素,并为其添加点击事件监听器。 4. 在点击向左按钮事件的监听器中,通过改变旋转角度的值来实现向左旋转的效果,可以使用`transform`属性和`rotateY()`函数来实现。 5. 在点击向右按钮事件的监听器中,通过改变旋转角度的值来实现向右旋转的效果,同样使用`transform`属性和`rotateY()`函数。 6. 最后,通过修改容器的样式,将旋转角度应用到容器上,从而实现图片的旋转效果。 需要注意的是,具体的实现可能还需要根据实际需求进行调整和优化,比如添加过渡效果、自动播放等。 综上所述,通过点击向左和向右按钮来改变旋转角度,可以实现HTML旋转木马轮播图效果。 #### 引用[.reference_title] - *1* *2* *3* [旋转木马轮播图 html+css+js](https://blog.csdn.net/luo1831251387/article/details/115150938)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北极光之夜。

谢谢~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值