首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript/jquery自动滚动轮播

问Javascript/jquery自动滚动轮播
EN

Stack Overflow用户
提问于 2014-07-23 19:30:04
回答 1查看 182关注 0票数 2

我想知道如何使用javascript使下面的内容自动滚动?carousol将使用HTML框放在google网站上,所以我不能只下载jquery插件,这将是最简单的解决方案!脚本中包含Jquery。任何帮助或建议都将不胜感激!代码如下:

代码语言:javascript
复制
<style>
  .carousel {
  width: 1080px;
  height: 220px;
  position: relative;
  overflow: hidden;
  background-color:black;
  border-radius: 20px;
  -moz-border-radius: 20px;
  -webkit-border-radius: 20px;
  margin-bottom: 20px;
  margin-top: 20px;
 }
 .items {
  width: 1080px;
  position: absolute;
 }
 .items > div {
  font-size: 20px;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
 }
 .items > div > img {
  width: 340px;
  height: 202px;
  padding: 10px;
 }
 .nav {
  position: absolute;
  bottom: 5px;
  right: 15px;
 }
 .button {
  cursor: pointer;
  font-weight: bold;
  color: #fff;
 }
</style>
<div class="carousel" style="display:none;">
 <div class="items">
  <div>
    <img src="http://i58.tinypic.com/2wq5nkk.png" border="0" alt="Speaker at event">
  </div>

  <div>
   <img src="http://i62.tinypic.com/vfii61.png" border="0" alt="Speaker at event">
  </div>
  <div>
    <img src="http://i59.tinypic.com/5ttg0z.png" border="0" alt="Speaker at event">
  </div>
  <div>
    <img src="http://i61.tinypic.com/okpq9g.png" border="0" alt="Speaker at event">
  </div>

  <div>
    <img src="http://i62.tinypic.com/2h4ywbo.png" border="0" alt="Speaker at event">
  </div>
  <div>
    <img src="http://i60.tinypic.com/21oyg4x.png" border="0" alt="Speaker at event">
  </div>
 </div>
 <div class="nav">
  <span class="button left-button">prev</span>
  &nbsp;&nbsp;
  <span class="button right-button">next</span>
 </div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"> </script>
<script>
 var current_slide = 0; // zero-based
 var slide_count = 2;
 var slide_size = 1080;

 var Direction = {
  LEFT: -1,
  RIGHT: 1
 };

 /**
 * Moves to the next slide using the direction (dx) parameter.
 */
 var nextSlide = function(dx) {
  current_slide = (current_slide + slide_count + dx) % slide_count;

  // Calculate the new value for css 'left' property and animate.
  var left_offset = '-' + (current_slide * slide_size) + 'px';
  $('.items').animate({'left': left_offset}, 1080);
 };

 $('.right-button').click(nextSlide.bind(null, Direction.RIGHT));
 $('.left-button').click(nextSlide.bind(null, Direction.LEFT));


 $('.carousel').show();
</script>
EN

回答 1

Stack Overflow用户

发布于 2014-07-23 19:46:43

最简单的方法是添加一个间隔,一遍又一遍地调用nextSlide。

代码语言:javascript
复制
var nextSlide = function(dx) {
  current_slide = (current_slide + slide_count + dx) % slide_count;

  // Calculate the new value for css 'left' property and animate.
  var left_offset = '-' + (current_slide * slide_size) + 'px';
  $('.items').animate({'left': left_offset}, 1080);
 };

 $('.carousel').show();

setInterval(function(){
    nextSlide(Direction.LEFT);
}, 5000); // Will call nextSlide every 5 seconds
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24909301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档