• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    HTML+CSS实现全景轮播的示例代码

    发布者: 嘉神 | 发布时间: 2025-8-17 00:59| 查看数: 83| 评论数: 0|帖子模式

    效果演示

    1. 实现了一个简单的网页布局,其中包含了五个不同的盒子,每个盒子都有一个不同的背景图片,并且它们之间有一些间距。当鼠标悬停在某个盒子上时,它的背景图片会变暗,并且文字会变成白色。这些盒子和按钮都被放在一个容器中,整个页面看起来像一个画廊。
    复制代码
    Code
    1. <div class="container">
    2.     <div id="slide">
    3.         <div class="item" style="background-image:url('./img/章若楠01.jpg')"></div>
    4.         <div class="item" style="background-image:url('./img/鞠婧祎01.jpg')"></div>
    5.         <div class="item" style="background-image:url('./img/鞠婧祎02.jpg')"></div>
    6.         <div class="item" style="background-image:url('./img/鞠婧祎06.jpg')"></div>
    7.         <div class="item" style="background-image:url('./img/鞠婧祎04.jpg')"></div>
    8.         <div class="item" style="background-image:url('./img/鞠婧祎07.jpg')"></div>
    9.     </div>
    10.     <div class="buttons">
    11.         <div class="left">
    12.             < Prev</div>
    13.                 <div class="center">下载壁纸</div>
    14.                 <div class="right">Next ></div>
    15.         </div>
    16.     </div>
    17. </div>
    复制代码
    1. * {
    2.     margin: 0;
    3.     padding: 0;
    4.     box-sizing: border-box;
    5. }
    6. .container {
    7.     width: 100vw;
    8.     height: 100vh;
    9.     position: relative;
    10.     overflow: hidden;
    11. }
    12. .item {
    13.     width: 240px;
    14.     height: 160px;
    15.     position: absolute;
    16.     top: 50%;
    17.     left: 0;
    18.     transform: translateY(-50%);
    19.     border-radius: 10px;
    20.     box-shadow: 0 30px 50px #505050;
    21.     background-size: cover;
    22.     background-position: center;
    23.     transition: 1s;
    24. }
    25. .item:nth-child(1),
    26. .item:nth-child(2) {
    27.     left: 0;
    28.     top: 0;
    29.     width: 100%;
    30.     height: 100%;
    31.     transform: translateY(0);
    32.     box-shadow: none;
    33.     border-radius: 0;
    34. }
    35. .item:nth-child(3) {
    36.     left: 70%;
    37. }
    38. .item:nth-child(4) {
    39.     left: calc(70% + 250px);
    40. }
    41. .item:nth-child(5) {
    42.     left: calc(70% + 500px);
    43. }
    44. .item:nth-child(n+6) {
    45.     left: calc(70% + 750px);
    46.     opacity: 0;
    47. }
    48. .buttons {
    49.     width: 100%;
    50.     position: absolute;
    51.     bottom: 50px;
    52.     margin-left: -50px;
    53.     text-align: center;
    54.     display: flex;
    55.     justify-content: center;
    56. }
    57. .buttons div {
    58.     width: 120px;
    59.     height: 50px;
    60.     line-height: 50px;
    61.     text-align: center;
    62.     border-radius: 5px;
    63.     margin: 0 25px;
    64.     transition: .5s;
    65.     cursor: pointer;
    66.     user-select: none;
    67.     font-size: 20px;
    68.     color: #fff;
    69.     background-color: rgba(0, 0, 0, 0.4);
    70.     box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.2);
    71. }
    复制代码
    1. const leftBtn = document.querySelector(".buttons .left")
    2. const rightBtn = document.querySelector(".buttons .right")
    3. const slide = document.querySelector("#slide")
    4. let openClick = true // 节流处理 (保证动画执行过程,按钮不被重复点击)
    5. leftBtn.addEventListener("click", () => {
    6.   if (openClick) {
    7.     openClick = false // 触发点击后,禁用按钮
    8.     const items = document.querySelectorAll(".item")
    9.     slide.prepend(items[items.length - 1])
    10.     setTimeout(() => openClick = true, 1000) // 1s 再开放按钮的点击
    11.   }
    12. })
    13. rightBtn.addEventListener("click", () => {
    14.   if (openClick) {
    15.     openClick = false
    16.     const items = document.querySelectorAll(".item")
    17.     slide.appendChild(items[0])
    18.     setTimeout(() => openClick = true, 1000)
    19.   }
    20. })
    复制代码
    实现思路拆分
    1. * {
    2.   margin: 0;
    3.   padding: 0;
    4.   box-sizing: border-box;
    5. }
    复制代码
    这段代码是设置全局的CSS样式,包括设置元素的盒模型为border-box,即盒模型的宽度和高度包括了元素的边框和内边距,而不是只包括元素的内容。
    1. .container {
    2.   width: 100vw;
    3.   height: 100vh;
    4.   position: relative;
    5.   overflow: hidden;
    6. }
    复制代码
    这段代码是设置容器的CSS样式,包括设置容器的宽度和高度为100vw和100vh,即视口的宽度和高度。同时,设置容器的定位为相对定位,即相对于其父元素进行定位。最后,设置容器的溢出属性为隐藏,即超出容器范围的元素不会被显示出来。
    1. .item {
    2.   width: 240px;
    3.   height: 160px;
    4.   position: absolute;
    5.   top: 50%;
    6.   left: 0;
    7.   transform: translateY(-50%);
    8.   border-radius: 10px;
    9.   box-shadow: 0 30px 50px #505050;
    10.   background-size: cover;
    11.   background-position: center;
    12.   transition: 1s;
    13. }
    复制代码
    这段代码是设置盒子的CSS样式,包括设置盒子的宽度和高度为240px和160px,即盒子的大小。同时,设置盒子的定位为绝对定位,即相对于其父元素进行定位。最后,设置盒子的边框半径为10px,即盒子的圆角。盒子的背景图片大小为cover,即覆盖整个盒子。背景图片的位置为居中对齐。最后,设置盒子的过渡效果为1秒,即过渡效果的时间为1秒。
    1. .item:nth-child(1),
    2. .item:nth-child(2) {
    3.   left: 0;
    4.   top: 0;
    5.   width: 100%;
    6.   height: 100%;
    7.   transform: translateY(0);
    8.   box-shadow: none;
    9.   border-radius: 0;
    10. }
    复制代码
    这段代码是设置第一个和第二个盒子的CSS样式,包括将它们的位置设置为0,即它们覆盖在容器的最上层。同时,将它们的高度设置为100%,即它们覆盖在容器的整个高度。最后,将它们的变换属性设置为 translateY(0),即它们不会向下移动。同时,将它们的阴影和边框半径设置为0,即它们没有阴影和边框。
    1. .item:nth-child(3) {
    2.   left: 70%;
    3. }
    复制代码
    这段代码是设置第三个盒子的CSS样式,包括将它的位置设置为距离容器左侧70%的位置。
    1. .item:nth-child(4) {
    2.   left: calc(70% + 250px);
    3. }
    复制代码
    这段代码是设置第四个盒子的CSS样式,包括将它的位置设置为距离第三个盒子右侧250px的位置。
    1. .item:nth-child(5) {
    2.   left: calc(70% + 500px);
    3. }
    复制代码
    这段代码是设置第五个盒子的CSS样式,包括将它的位置设置为距离第三个盒子右侧500px的位置。
    1. .item:nth-child(n+6) {
    2.   left: calc(70% + 750px);
    3.   opacity: 0;
    4. }
    复制代码
    这段代码是设置所有盒子的CSS样式,包括将它们的位置设置为距离第三个盒子右侧750px的位置。同时,将它们的不透明度设置为0,即它们不可见。
    1. .buttons {
    2.   width: 100%;
    3.   position: absolute;
    4.   bottom: 50px;
    5.   margin-left: -50px;
    6.   text-align: center;
    7.   display: flex;
    8.   justify-content: center;
    9. }
    复制代码
    这段代码是设置按钮的CSS样式,包括设置按钮的宽度为100%,即按钮的大小与容器相同。同时,将按钮的位置设置为距离容器底部50px的位置。最后,将按钮的对齐方式设置为居中对齐,即按钮在水平方向上居中对齐。
    1. .buttons div {
    2.     width: 120px;
    3.     height: 50px;
    4.     line-height: 50px;
    5.     text-align: center;
    6.     border-radius: 5px;
    7.     margin: 0 25px;
    8.     transition:.5s;
    9.     cursor: pointer;
    10.     user-select: none;
    11.     font-size: 20px;
    12.     color: #fff;
    13.     background-color: rgba(0, 0, 0, 0.4);
    14.     box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.2);
    15. }
    复制代码
    这段代码是设置按钮的CSS样式,包括设置按钮的宽度为120px,高度为50px,即按钮的大小。同时,设置按钮的行高为50px,即按钮的高度。按钮的文本对齐方式为居中对齐,即文本在水平方向上居中对齐。按钮的边框半径为5px,即按钮的圆角。按钮的外边距为0 25px,即按钮在水平方向上左右两侧的距离为25px。按钮的过渡效果为0.5秒,即过渡效果的时间为0.5秒。按钮的光标属性为pointer,即鼠标悬停在按钮上时,鼠标的形状会变成手型。按钮的用户选择属性为none,即用户不能选中按钮中的文本。按钮的字体大小为20px,即按钮的文本大小。按钮的文本颜色为白色,即按钮的文本颜色。按钮的背景颜色为rgba(0, 0, 0, 0.4),即按钮的背景颜色为黑色,透明度为0.4。按钮的阴影属性为2px 2px 2px rgba(0, 0, 0, 0.2),即按钮的阴影为2px 2px 2px黑色,透明度为0.2。
    到此这篇关于HTML+CSS实现全景轮播的示例代码的文章就介绍到这了,更多相关HTML CSS全景轮播内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表