Bootstrap的图片轮播示例代码_javascript技巧
示例一:
插入js及css支持:
<link rel="stylesheet" href="css/bootstrap.min.css"/> <script src="js/jquery-1.9.1.min.js"></script> <script src="js/bootstrap.min.js"></script>
HTML代码:
<div id="pictures" class="item"> <div id="myCarousel" class="carousel slide"> <!-- 轮播(Carousel)指标 --> <ol class="carousel-indicators"> <li data-target="#myCarousel" data-slide-to="0" class="active"></li> <li data-target="#myCarousel" data-slide-to="1"></li> <li data-target="#myCarousel" data-slide-to="2"></li> </ol> <!-- 轮播(Carousel)项目 --> <div class="carousel-inner"> <div class="item active"> <img class="img-responsive lazy" src="/static/imghw/default1.png" data-src="images/gf.jpg" alt="First slide"> </div> <div class="item"> <img class="img-responsive lazy" src="/static/imghw/default1.png" data-src="images/psb.jpg" alt="Second slide"> </div> <div class="item"> <img class="img-responsive lazy" src="/static/imghw/default1.png" data-src="images/uyt.jpg" alt="Third slide"> </div> </div> <!-- 轮播(Carousel)导航 --> <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a> </div> </div>
示例二:
用法
<div id="myCarousel" class="carousel slide"> <!-- Carousel items --> <div class="carousel-inner"> <div class="active item">…</div> <div class="item">…</div> <div class="item">…</div> </div> <!-- Carousel nav --> <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a> </div>
所以,您把想要呈现的条目(比如 images)以循环顺序放置在 "carousel-inner" div 中,通过 "" 创建条目的导航。它使用定制的 data 属性 "data-slide" 来导航到上一个和下一个条目。
您必须在您要创建轮播的 HTML 文件引用 jquery.js 和 bootstrap-carousel.js 文件。
Bootstrap 轮播实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Twitter Bootstrap pager with next and previous example</title> <meta name="description" content="Twitter Bootstrap pager with next and previous example"> <link href="/twitter-bootstrap/twitter-bootstrap-v2/docs/assets/css/bootstrap.css" rel="stylesheet"> <style type="text/css"> body { margin: 50px; } </style> </head> <body> <ul class="pager"> <li> <a href="#">Previous</a> </li> <li> <a href="#">Next</a> </li> </ul> </body> </html>
带有 old 和 new 的翻页实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Example of carousal with Twitter Bootstrap</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="Example of carousal with Twitter Bootstrap version 2.0 from w3resource.com"> <!-- Le styles --> <link href="twitter-bootstrap-v2/docs/assets/css/bootstrap.css" rel="stylesheet"> <link href="twitter-bootstrap-v2/docs/assets/css/example-fixed-layout.css" rel="stylesheet"> <!-- Le HTML5 shim, for IE6-8 support of HTML5 elements --> <!--[if lt IE 9]> <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <!-- Le fav and touch icons --> <link rel="shortcut icon" href="twitter-bootstrap-v2/docs/examples/images/favicon.ico"> <link rel="apple-touch-icon" href="twitter-bootstrap-v2/docs/examples/images/apple-touch-icon.png"> <link rel="apple-touch-icon" sizes="72x72" href="twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-72x72.png"> <link rel="apple-touch-icon" sizes="114x114" href="twitter-bootstrap-v2/docs/examples/images/apple-touch-icon-114x114.png"> </head> <body> <div class="navbar navbar-fixed-top"> <div class="navbar-inner"> <div class="container"> <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </a> <a class="brand" href="#"><img src="/static/imghw/default1.png" data-src="/images/w3r.png" class="lazy" style="max-width:90%" style="max-width:90%" alt="w3resource logo" /></a> <div class="nav-collapse"> <ul class="nav"> <li class="active"><a href="#">Home</a></li> <li><a href="#about">About</a></li> <li><a href="#contact">Contact</a></li> </ul> </div><!--/.nav-collapse --> </div> </div> </div> <div class="container"> <!-- Example row of columns --> <div class="row"> <div class="span4"> <h2 id="HTML-and-JS-Apps">HTML5 and JS Apps</h2> <p> </p> <div id="myCarousel" class="carousel slide"> <!-- Carousel items --> <div class="carousel-inner"> <div class="active item"><img src="/static/imghw/default1.png" data-src="/update-images/html5_logo.png" class="lazy" alt="HTML5 logo" style="max-width:90%" style="max-width:90%" /></div> <div class="item"><img src="/static/imghw/default1.png" data-src="/update-images/javascript-logo.png" class="lazy" alt="JS logo" style="max-width:90%" style="max-width:90%" /></div> <div class="item"><img src="/static/imghw/default1.png" data-src="/update-images/schema.png" class="lazy" alt="Schema.org logo" style="max-width:90%" style="max-width:90%" /></div> <div class="item"><img src="/static/imghw/default1.png" data-src="/update-images/json.gif" class="lazy" alt="JSON logo" style="max-width:90%" style="max-width:90%" /></div> </div> <!-- Carousel nav --> <a class="carousel-control left" href="#myCarousel" data-slide="prev">‹</a> <a class="carousel-control right" href="#myCarousel" data-slide="next">›</a> </div> </div> </div> <hr> <footer> <p>© Company 2012</p> </footer> </div> <!-- /container --> <!-- Le javascript ================================================== --> <!-- Placed at the end of the document so the pages load faster --> <script src="twitter-bootstrap-v2/docs/assets/js/jquery.js"></script> <script src="twitter-bootstrap-v2/docs/assets/js/bootstrap-carousel.js"></script> </body> </html>
使用 Javascript
您可以使用下面的 JavaScript 代码来创建轮播。
$('.carousel').carousel()
下面是您可以使用的选项
interval: 规定幻灯片轮换的等待时间,以毫秒为单位。值的类型为 number,默认值是 5000。如果为 false,轮播将不会自动开始循环。
pause: 规定当鼠标停留在幻灯片区域即暂停轮播,鼠标离开即启动轮播。值的类型为 string,默认值是 'hover'。
下面是您可以使用的轮播方法
.carousel(options): 初始化轮播组件,接受一个可选的 object 类型的 options 参数,并开始幻灯片循环。 $('.carousel').carousel({ interval: 2000 // in milliseconds }) .carousel('cycle'): 从左到右循环各帧。 $('.carousel').carousel('cycle'); .carousel('pause'): 停止轮播。 $('#myCarousel').hover(function () { $(this).carousel('pause') } .carousel(number): 将轮播定位到指定的帧上(帧下标以0开始,类似数组)。 $("#carousel_nav").click(function(){ var item = 4; $('#home_carousel').carousel(item); return false; });
.carousel('prev'): 将轮播转到上一帧。
.carousel('next'): 将轮播转到下一帧。
这里有两个事件用来增强轮播的功能。
slide: 当 slide 实例方法被调用之后,此事件被立即触发。
slid: 当所有幻灯片播放完之后,此事件被触发。

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题











Python和JavaScript开发者的薪资没有绝对的高低,具体取决于技能和行业需求。1.Python在数据科学和机器学习领域可能薪资更高。2.JavaScript在前端和全栈开发中需求大,薪资也可观。3.影响因素包括经验、地理位置、公司规模和特定技能。

JavaScript是现代Web开发的基石,它的主要功能包括事件驱动编程、动态内容生成和异步编程。1)事件驱动编程允许网页根据用户操作动态变化。2)动态内容生成使得页面内容可以根据条件调整。3)异步编程确保用户界面不被阻塞。JavaScript广泛应用于网页交互、单页面应用和服务器端开发,极大地提升了用户体验和跨平台开发的灵活性。

如何在JavaScript中将具有相同ID的数组元素合并到一个对象中?在处理数据时,我们常常会遇到需要将具有相同ID�...

实现视差滚动和元素动画效果的探讨本文将探讨如何实现类似资生堂官网(https://www.shiseido.co.jp/sb/wonderland/)中�...

深入探讨console.log输出差异的根源本文将分析一段代码中console.log函数输出结果的差异,并解释其背后的原因。�...

学习JavaScript不难,但有挑战。1)理解基础概念如变量、数据类型、函数等。2)掌握异步编程,通过事件循环实现。3)使用DOM操作和Promise处理异步请求。4)避免常见错误,使用调试技巧。5)优化性能,遵循最佳实践。

探索前端中类似VSCode的面板拖拽调整功能的实现在前端开发中,如何实现类似于VSCode...
