jQuery Implementation Bulletin News Automatic Scroll Screen Effect Example Code

  • 2021-07-02 23:33:54
  • OfStack

This article is the site on the reference network of a small demo, their own expansion, the original is scrolling up, scrolling down the expansion of a method, the specific example code is as follows:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> Scroll screen experiment </title>
<style type="text/css">
ul,li{margin:0;padding:0}
#scrollDiv{width:300px;height:100px;min-height:25px;line-height:25px;border:#ccc 1px solid;overflow:hidden}
#scrollDiv li{height:25px;padding-left:10px;}
</style>
<script src="js/jquery-1.4.1.js"></script>
<script type="text/javascript">
(function ($) {
$.fn.extend({
Scroll: function (opt, callback) {
// Parameter initialization 
if (!opt) var opt = {};
var _this = this.eq(0).find("ul:first");
var lineH = _this.find("li:first").height(), // Get row height 
line = opt.line ? parseInt(opt.line, 10) : parseInt(this.height() / lineH, 10), // The number of rows to scroll at a time, the default is 1 Screen, that is, parent container height 
speed = opt.speed ? parseInt(opt.speed, 10) : 500, // Scroll speed, the larger the value, the slower the speed (milliseconds) 
timer = opt.timer ? parseInt(opt.timer, 10) : 2000; // Time interval for scrolling (milliseconds) 
if (line == 0) line = 1;
var upHeight = 0 - line * lineH;
var downHeight=line * lineH - 0;
// Rolling function 
scrollUp = function () {
_this.animate(
{ marginTop: upHeight },
speed,
function () {
for (i = 1; i <= line; i++) {
_this.find("li:first").appendTo(_this);
}
_this.css({ marginTop: 0 });
}
);
},
// Scroll down function 
scrollDown = function () {
_this.animate(
{ marginTop: downHeight },// Animation display css Style 
speed,
function () {
_this.find("li:last").prependTo(_this);
_this.css({ marginTop: 0 });
}
)
}
var timerID
// Mouse event binding 
_this.hover(function () {
clearInterval(timerID);
}, function () {
timerID = setInterval("scrollDown()", timer);// The scroll down or up function is called here 
}).mouseout();
}
})
})(jQuery);
$(document).ready(function () {
$("#scrollDiv").Scroll({ line: 1, speed: 500, timer: 2000 });
});
</script>
</head>
<body>
<p> Multi-line scrolling demonstration: </p>
<div id="scrollDiv">
<ul>
<li> This is the title of the announcement 1 Row </li>
<li> This is the title of the announcement 2 Row </li>
<li> This is the title of the announcement 3 Row </li>
<li> This is the title of the announcement 4 Row </li>
<li> This is the title of the announcement 5 Row </li>
<li> This is the title of the announcement 6 Row </li>
<li> This is the title of the announcement 7 Row </li>
<li> This is the title of the announcement 8 Row </li>
</ul>
</div>
</body>
</html>

Related articles: