Jquery: how to loop a div

using jquery, how can I automatically scroll through a div? as a news section and features of this site: http://animalsasia.org/ . And also, when you hover over the slider, it stops scrolling until you leave.

is there a jquery plugin that will do this? Any help would be greatly appreciated.

+3
source share
4 answers

I wrote some working examples. JsFiddle shows a live example. The idea is to create a container with position = relative and place a div with text in it. In addition, we need to create a copy of the text to avoid empty space when the last part of the text is displayed. The jQuery animate () function will do the rest.

HTML:

<div class="news_container">
    <div class="news">
       <div class="text">
           Long text
        </div>   
    </div>
</div>

CSS

.news_container {
  border: 1px solid black;
  width:150px;
  height: 300px;   
  overflow: hidden;
  position: relative;
  padding: 3px;
}

.news {
  position: absolute; 
  left: 0px;
  top: 0px;
}

JavaScript:

(function($, undefined) {
  $.fn.loopScroll = function(p_options) {
    var options = $.extend({
        direction: "upwards",
        speed: 60
    }, p_options);

    return this.each(function() {
      var obj = $(this).find(".news");
      var text_height = obj.find(".text").height();
      var start_y, end_y;
      if (options.direction == "downwards") {
        start_y = -text_height;
        end_y = 0;
      } else if (options.direction == "upwards") {
        start_y = 0;
        end_y = -text_height;
      }

      var animate = function() {
        // setup animation of specified block "obj"
        // calculate distance of animation    
        var distance = Math.abs(end_y - parseInt(obj.css("top")));

        //duration will be distance / speed
        obj.animate(
          { top: end_y },  //scroll upwards
          1000 * distance / options.speed,
          "linear",
          function() {
            // scroll to start position
            obj.css("top", start_y);
            animate();    
          }
        );
      };

      obj.find(".text").clone().appendTo(obj);
      $(this).on("mouseover", function() {
        obj.stop();
      }).on("mouseout", function() {
        animate(); // resume animation
      });
      obj.css("top", start_y);
      animate(); // start animation       
    });
  };
}(jQuery));

$(".news_container").loopScroll();

Parameters:

  • direction ("down" or "up") - the direction of text movement;
  • speed - speed of movement.

Here is an example of using this plugin with parameters:

$("#example3").loopScroll();
$("#example4").loopScroll({ speed: 120 });
$("#example1").loopScroll({ direction: "downwards" });
$("#example2").loopScroll({ direction: "downwards", speed: 30 });
+14
source

, DIV . , setInterval top DIV . DIV ( DIV), , , . DIVs , , , .

+2

, , top.

1 .

------
item 1
item 2
item 3
------
item 4

1

item 1
-----
item 2
item 3
item 4
------

1 ,

-----
item 2
item 3
item 4
-----
item 1

This is pretty trivial to implement, but jquery vticker should contain the desired functionality.

+2
source

You can use the marquee html tag (non-standard, but should work almost everywhere) or check out this jquery plugin:

http://remysharp.com/2008/09/10/the-silky-smooth-marquee/

0
source

All Articles