Not able to slide the div content using jquery

I’m trying to move the images inside a div to the left one by one.But i’m only able to move one image on click of a button.I’m looking to slide the images inside a div. Here is my code on jsfiddle.

Problem courtesy of: iJade


jsFiddle demo

  • Once you reach the last image it would be goot to turn your slider to the starting position, so let’s use Modulus
    to turn our counter to ‘0’.
  • Than just move your slider doing some basic math: -300px * the current Counter value

After each click counter will increase, but once it reaches the number of images 4
it will jump back to value 0
300px * 0 = 0
and that will slide your gallery back to ‘0px’ left.


var imgN = $('#slide div img').length; // number of images
var c = 0; // just a Counter
$("#btn").click(function() {
    c = ++c % imgN;      // once it reaches the imgN will turn to '0'
    $("#slide div").animate({left: -300*c }, "fast");   // let's use our math!

Modified CSS:

        height: 300px;
        overflow: hidden;
    #slide div{
        position: absolute;
        left: 0px;
        width:9000px;    /*make sure to have a big value here if you don't want to do it dynamically with jQUery*/
   #slide img{
        vertical-align: top;
        height: 300px;
        width: 300px;

Solution courtesy of: Roko C. Buljan

稿源:CSS3 Recipes (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Not able to slide the div content using jquery

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录