css+js实现banner图片轮播

1. 摘要

css实现banner布局,js实现图片轮播,附带按钮二级效果、banner响应式缩放。


2. html部分

  • 从左往右依次放置按钮、图片、按钮,两个按钮和图片处于同一div中。
<!-- banner --> 
<div class="div_banner" onmouseover="bannerOver()" onmouseout="bannerOut()"> 
    <button class="btn_shift" name="btn_shift" onclick="clickPrev()">&lt;</button> 
        <img class="img_banner" id="banner" src="banner_internship.jpeg" width="1000" height="600" /> 
    <button class="btn_shift" name="btn_shift" onclick="clickNext()">&gt;</button> 
</div>

3. css部分

  • 两个按钮和图片所处的div设置子控件垂直居中。
  • 按钮的层级一定要在图片之上,即z-index值更大。
  • 图片设置flex为1。
  • 图片margin值为负数,大小等于按钮宽度。
.div_banner {
    width: 100%;
    height: auto;
    padding: 0;
    display: -webkit-flex;
    display: flex;
    align-items: center;
}

.btn_shift {
    width: 40px;
    height: 80px;
    margin: 0;
    padding: 0;
    border: none;
    display: inline-block;
    text-align: center;
    align-items: center;
    text-decoration: none;
    font-size: 35px;
    visibility: hidden;
    background-color: #111;
    opacity: 0.7;
    -webkit-transition-duration: 0.5s;
    transition-duration: 0.5s;
    cursor: pointer;
    z-index: 8999;
}

.btn_shift:hover {
    background-color: #efefef;
    opacity: 0.7;
}

.img_banner {
    width: 100%;
    height: 100%;
    max-height: 600px;
    flex: 1;
    margin-left: -40px;
    margin-right: -40px;
    cursor: pointer;
}

4. js部分

  • 以json格式存放banner图片资源。
  • 通过visibility值来控制按钮是否隐藏。
  • 使用setInterval来实现图片轮播,点击按钮切换图片时,先clearInterval再重新启动。
/********** 常量 **********/

/**
 * banner切换时间间隔
 */
var timeout = 5000;

/**
 * banner下标
 */
var bannerIndex = 1000;

/**
 * banner定时器
 */
var bannerInterval;

/**
 * banner内容json
 */
var bannerJson = [{
    "imgUrl": "banner_internship.jpeg",
    "desUrl": "https://frogfans.github.io/blog.html?blogId=18"
},
{
    "imgUrl": "banner_recruit.jpg",
    "desUrl": "https://frogfans.github.io/blog.html?blogId=18"
}
];

/********** 函数 **********/

/**
 * banner单次执行内容
 */
function interval() {
    if(bannerIndex > 1000) bannerIndex -= 1000;
    if(bannerIndex < 0) bannerIndex += 1000;
    var banner = document.getElementById("banner");
    banner.src = bannerJson[bannerIndex % bannerJson.length].imgUrl;
    banner.setAttribute("desUrl", bannerJson[bannerIndex % bannerJson.length].desUrl)
    banner.onclick = function(){
        window.open(this.getAttribute("desUrl"));
    }

    bannerIndex++;
}

/**
 * banner启动
 */
function startBanner() {
    interval();
    bannerInterval = setInterval('interval()', timeout);
}

/**
 * 鼠标悬停banner
 */
function bannerOver() {
    var btn = document.getElementsByName('btn_shift');
    for (var i = 0; i < btn.length; i++) {
        btn[i].style = 'visibility:visible;';
    }
}

/**
 * 鼠标移出banner
 */
function bannerOut() {
    var btn = document.getElementsByName('btn_shift');
    for (var i = 0; i < btn.length; i++) {
        btn[i].style = 'visibility:hidden;';
    }
}

/**
 * 点击上一项
 */
function clickPrev() {
    clearInterval(bannerInterval);
    bannerIndex-=2;
    startBanner();
}

/**
 * 点击下一项
 */
function clickNext() {
    clearInterval(bannerInterval);
    startBanner();
}

5. 效果图


6. 在线体验

Banner-Demo on-line


7. 进阶


8. 源码

Banner-Demo

  • 3
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
好的,我可以提供一个简单的示例,但这可能远远超过300行。请注意,这只是一个基本的框架,您需要根据自己的需求进行修改和定制。 HTML代码: ```html <!DOCTYPE html> <html> <head> <title>旅游网页</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <div class="container"> <h1>旅游网页</h1> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">旅游指南</a></li> <li><a href="#">景点介绍</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> </div> </header> <section id="banner"> <div class="container"> <h2>发现你的理想旅游目的地</h2> <p>探索世界各地的美景和文化</p> <a href="#" class="btn">了解更多</a> </div> </section> <section id="features"> <div class="container"> <h3>我们的旅游服务</h3> <div class="feature"> <img src="img/feature1.jpg"> <h4>行程定制</h4> <p>根据您的需求和预算,我们可以为您设计最佳的旅游行程</p> </div> <div class="feature"> <img src="img/feature2.jpg"> <h4>导游服务</h4> <p>我们的专业导游将为您提供全面的旅游指导和解说服务</p> </div> <div class="feature"> <img src="img/feature3.jpg"> <h4>住宿安排</h4> <p>我们将为您安排最舒适的住宿环境,让您的旅途更加愉快</p> </div> </div> </section> <section id="destinations"> <div class="container"> <h3>热门旅游目的地</h3> <div class="destination"> <img src="img/destination1.jpg"> <h4>巴黎</h4> <p>浪漫之都,充满了艺术和文化的气息</p> <a href="#" class="btn">了解更多</a> </div> <div class="destination"> <img src="img/destination2.jpg"> <h4>东京</h4> <p>现代化城市,古老的文化和新潮的时尚完美结合</p> <a href="#" class="btn">了解更多</a> </div> <div class="destination"> <img src="img/destination3.jpg"> <h4>夏威夷</h4> <p>阳光海滩,美丽的自然风光和热情的人民</p> <a href="#" class="btn">了解更多</a> </div> </div> </section> <footer> <div class="container"> <p>版权所有 © 2021 旅游网页</p> <nav> <ul> <li><a href="#">使用条款</a></li> <li><a href="#">隐私政策</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> </div> </footer> <script src="script.js"></script> </body> </html> ``` CSS代码: ```css /* 全局样式 */ * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 16px; color: #333; } a { color: #337ab7; text-decoration: none; } a:hover { text-decoration: underline; } /* 头部样式 */ header { background-color: #f1f1f1; padding: 20px 0; } .container { width: 80%; margin: 0 auto; } nav ul { list-style: none; display: flex; } nav li { margin-right: 20px; } nav li:last-child { margin-right: 0; } nav a { font-weight: bold; } /* 横幅样式 */ #banner { background-image: url('img/banner.jpg'); background-size: cover; background-position: center center; height: 500px; display: flex; align-items: center; justify-content: center; } #banner h2 { font-size: 48px; color: #fff; text-align: center; margin-bottom: 20px; } #banner p { font-size: 24px; color: #fff; text-align: center; margin-bottom: 40px; } .btn { display: inline-block; background-color: #337ab7; color: #fff; padding: 10px 20px; border-radius: 5px; } .btn:hover { background-color: #23527c; } /* 特色服务样式 */ #features { padding: 80px 0; background-color: #f9f9f9; } #features h3 { font-size: 36px; margin-bottom: 40px; text-align: center; } .feature { width: 33.33%; text-align: center; padding: 0 20px; display: inline-block; vertical-align: top; } .feature img { width: 100%; } .feature h4 { font-size: 24px; margin-top: 20px; margin-bottom: 10px; } .feature p { font-size: 16px; color: #666; margin-bottom: 20px; } /* 热门旅游目的地样式 */ #destinations { padding: 80px 0; } #destinations h3 { font-size: 36px; margin-bottom: 40px; text-align: center; } .destination { width: 33.33%; text-align: center; padding: 0 20px; display: inline-block; vertical-align: top; } .destination img { width: 100%; } .destination h4 { font-size: 24px; margin-top: 20px; margin-bottom: 10px; } .destination p { font-size: 16px; color: #666; margin-bottom: 20px; } /* 底部样式 */ footer { background-color: #333; color: #fff; padding: 20px 0; } footer p { text-align: center; margin-bottom: 20px; } footer nav ul { list-style: none; display: flex; justify-content: center; } footer li { margin-right: 20px; } footer li:last-child { margin-right: 0; } ``` JavaScript代码: ```javascript // 在此添加JavaScript代码 // 比如轮播图、交互效果等 ``` 请注意,这只是一个基本的框架,需要您根据自己的需求进行修改和定制。同时,您还需要添加图片、JavaScript代码、样式表和其他资源文件,以便完整地呈现您的旅游网页。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值