jQuery实现图片向左向右切换效果的简单实例

所属分类: 网络编程 / JavaScript 阅读数: 1267
收藏 0 赞 0 分享

jQuery实现图片向左向右切换效果的简单实例

<div class="imageRotation container">
  <div class="imageBox">
    <img src="images/chugui.jpg">
    <img src="images/ad_auto.jpg">
    <img src="images/ad_home.jpg">
    <img src="images/ad_nba.jpg">
    <img src="images/ad_stock.jpg">
    <img src="images/ad_yuetu.jpg">
  </div>
  <div class="iconBox">
    <span rel="1" class="active">1</span>
    <span rel="2">2</span>
    <span rel="3">3</span>
    <span rel="4">4</span>
    <span rel="5">5</span>
    <span rel="6">6</span>
  </div>
</div>

 

//CSS样式

.container {
  width: 1000px;
  margin: 0 auto;
}


.imageRotation {
  width: 1000px;
  height: 480px;
  position: relative;
  overflow: hidden;
  margin-top: 8px;
}


.imageBox {
  position: absolute;
  overflow: hidden;
  display: block;
  height: 480px;
}


.imageBox img {
  width: 1000px;
  height: 480px;
  float: left;
  border: none;
  display: block;
}


.iconBox {
  position: absolute;
  width: 120px;
  height: 12px;
  line-height: 12px;
  top: 444px;
  right: 20px;
  text-align: center;
}


.iconBox span {
  width: 6px;
  height: 6px;
  border-radius: 10px;
  text-align: center;
  background: #555;
  border: 2px solid #f5f5f5;
  float: left;
  text-indent: -999em;
  margin-left: 5px;
  cursor: pointer;
}


.iconBox span.active {
  width: 6px;
  height: 6px;
  background: #820000;
  border-radius: 10px;
  text-align: center;
  text-indent: -999em;
}

 

//js逻辑

$(function() {
  $(".imageRotation").each(function() {
    var imageRotation = this,
    imageBox = $(imageRotation).children(".imageBox"), 
    iconBox = $(imageRotation).children(".iconBox"), 
    iconArr = $(iconBox).children(), 
    imageWidth = $(imageRotation).width(), 

    imageNum = $(imageBox).children().size(), 
    imageRollWidth = imageWidth * imageNum,

    activeID = parseInt($($(iconBox).children(".active")).attr("rel")),

    nextID = 0; 
    var setIntervalID,
    setIntervalTime = 3000,
    speed = 500;



  //设置 图片容器 的宽度
  $(imageBox).css({
    'width': imageRollWidth + "px"

  });

  //图片切换函数
  function imageRoll(clickID) {
    if (clickID) {
    nextID = clickID;
    } else {
      if (activeID <= 5) {
        nextID = activeID + 1
      } else {
    nextID = 1;
      }
    }

    //图标添加样式、删除样式
    $(iconArr[activeID - 1]).removeClass("active");
    $(iconArr[nextID - 1]).addClass("active");

    $(imageBox).animate({
      left: "-" + (nextID - 1) * imageWidth + "px"

    }, speed);
    activeID = nextID;
  }

  setIntervalID = setInterval(imageRoll, setIntervalTime);

  //鼠标移动事件
  $(imageBox).hover(function() {
    clearInterval(setIntervalID);
  }, function() {
  setIntervalID = setInterval(imageRoll, setIntervalTime);
});

    //鼠标点击事件
    $(iconArr).click(function() {
      clearInterval(setIntervalID);
      var clickID = parseInt($(this).attr("rel")); //获取当前点击图片的id
      imageRoll(clickID);
      setIntervalID = setInterval(imageRoll, setIntervalTime);
      });
   });
});

以上这篇jQuery实现图片向左向右切换效果的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Node.js事件的正确使用方法

这篇文章主要给大家介绍了关于Node.js事件的正确使用方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Node.js具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

微信小程序自定义导航栏实例代码

这篇文章主要给大家介绍了关于微信小程序自定义导航栏的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用微信小程序具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

js字符串处理之绝妙的代码

这篇文章主要介绍了js字符串处理之绝妙的代码,包括字符串去重、将字符放在对象中遍历拼接出字符串、把字符串的奇数位和偶数位分别提取到两个数组中、将两个数组按奇数位和偶数位插入拼接为字符串,有时候需要对字符进行处理的时候可以用得到
收藏 0 赞 0 分享

vue设计一个倒计时秒杀的组件详解

这篇文章主要介绍了vue设计一个倒计时秒杀的组件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解服务端预渲染之Nuxt(介绍篇)

这篇文章主要介绍了详解服务端预渲染之Nuxt(介绍篇),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

javascrit中undefined和null的区别详解

这篇文章主要介绍了javascrit中undefined和null的区别详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈javascript中的prototype和__proto__的理解

这篇文章主要介绍了浅谈javascript中的prototype和__proto__的理解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解vue中this.$emit()的返回值是什么

这篇文章主要介绍了详解vue中this.$emit()的返回值是什么,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

浅谈express.js框架中间件(middleware)

这篇文章主要介绍了浅谈express.js框架中间件(middleware),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

vue自定义指令directive的使用方法

这篇文章主要介绍了vue自定义指令directive的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多