jQuery不间断滚动效果(模拟百度新闻支持文字/图片/垂直滚动)


Posted in Javascript onFebruary 05, 2013

一、jQuery.roll 插件使用说明
jQuery.roll 是模拟百度新闻不间断滚动效果,并支持文字、图片水平垂直滚动,该函数使用方法为:

/* 
* @module jQuery roll 
* @param: contentCls 内容容器className 
* @param: contentParentId 内容容器父元素节点ID 
* @param: configs 配置参数 
* 
* @config: effect 滚动效果 
* @config: duration 滚动1个像素的运行时间(毫秒数) 
* @config: delay 开始滚动的延迟时间(毫秒数) 
* 
*/ 
jQuery.roll(contentCls, contentParentId, configs);

二、函数源码
jQuery.extend({ 
roll: function(contentCls, contentParentId, configs){ 
var setTimeID, totalWidth = 0, totalHeight = 0, 
firstContent, secondContent, contents; 
(function(){ 
var singleContent, cloneContent, nodeList; 
singleContent = $(contentCls, contentParentId); 
nodeList = singleContent.children(); 
if (configs.effect === 'scrollX') { 
$.each(nodeList, function(idx, itm) { 
totalWidth += $(itm).outerWidth(true); 
}); 
singleContent.css({ 'width': totalWidth + 'px' }); 
} 
else if (configs.effect === 'scrollY') { 
$.each(nodeList, function(idx, itm) { 
totalHeight += $(itm).outerHeight(true); 
}); 
singleContent.css({ 'height': totalHeight + 'px' }); 
} 
cloneContent = singleContent.clone(); 
cloneContent.appendTo(contentParentId); 
contents = $(contentCls, contentParentId); 
firstContent = contents[0]; 
secondContent = contents[1]; 
if (configs.effect === 'scrollX') { 
$(firstContent).css({ 'left': 0 }); 
$(secondContent).css({ 'left': totalWidth + 'px' }); 
} 
else if (configs.effect === 'scrollY') { 
$(firstContent).css({ 'top': 0 }); 
$(secondContent).css({ 'top': totalHeight + 'px' }); 
} 
})() 
function cssAnimate(){ 
if (configs.effect === 'scrollX') { 
$(firstContent).css({ left: parseInt(firstContent.style.left, 10) - 1 + 'px' }); 
$(secondContent).css({ left: parseInt(secondContent.style.left, 10) - 1 + 'px' }); 
$.each(contents, function(idx, itm) { 
if (parseInt(itm.style.left,10) === -totalWidth) { 
$(itm).css({ left: totalWidth + 'px' }); 
} 
}); 
} 
else if (configs.effect === 'scrollY') { 
$(firstContent).css({ top: parseInt(firstContent.style.top, 10) - 1 + 'px' }); 
$(secondContent).css({ top: parseInt(secondContent.style.top, 10) - 1 + 'px' }); 
$.each(contents, function(idx, itm) { 
if (parseInt(itm.style.top,10) === -totalHeight) { 
$(itm).css({ top: totalHeight + 'px' }); 
} 
}); 
} 
setTimeId = setTimeout(cssAnimate, configs.duration); 
} 
function rollRun(){ 
setTimeId = setTimeout(cssAnimate, configs.delay); 
return jQuery; 
} 
function rollStop(){ 
clearTimeout(setTimeId); 
return jQuery; 
} 
return $.extend({ 
rollRun: rollRun, 
rollStop: rollStop 
}); 
} 
});

三、完整demo源码
例3.1
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<meta name="description" content="jQuery实现无间隙滚动效果"> 
<title>jQuery demo</title> 
<style> 
body { font: 12px/1.5 tahoma,"microsoft yahei","微软雅黑E\8F6F\96C5\9ED1"; } 
body, div, ul, li, h1 { margin: 0; padding: 0; } 
.news { margin: 100px 0 0 100px; } 
.news ul { list-style: none; } 
.news-box { width: 600px; margin-left: 20px; height: 24px; background-color: #fcfcfd; overflow: hidden; position: relative; _zoom: 1; } 
.news h1 { margin-bottom: 15px; padding-left: 20px; color: #370188; } 
.news-list { position: absolute; } 
.news-list { float: left; } 
.news-list li { float: left; _display: inline; margin-right: 15px; height: 24px; line-height: 24px; overflow: hidden; word-wrap: normal; } 
.news-list li a { text-decoration: none; color: #000; } 
.news-list li a:hover { 
-webkit-transition: color .2s linear,background-color .3s linear; 
-moz-transition: color .2s linear,background-color .3s linear; 
-ms-transition: color .2s linear,background-color .3s linear; 
-o-transition: color .2s linear,background-color .3s linear; 
transition: color .2s linear,background-color .3s linear; 
color: #FF4400; 
text-decoration: underline; 
} 
.news-list li a:visited { color: #290065; } 
</style> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
</head> 
<body> 
<div id="J_news" class="news"> 
<h1>文字列表滚动</h1> 
<div id="J_Roll_Container" class="news-box"> 
<ul class="J_Roll_Content news-list"> 
<li><a href="http://news.sina.com.cn/c/2013-01-10/121926003955.shtml" target="_blank">日方否认将对中国巡航钓鱼岛飞机</a></li> 
<li><a href="http://news.hf365.com/system/2013/01/10/012875943.shtml" target="_blank">日本防卫省否认将对中国飞机警告射击(图)</a></li> 
<li><a href="http://news.21cn.com/world/guojisaomiao/2013/01/10/14338977.shtml" target="_blank">日否认警告射击中海监飞机 恐中日因夺岛开战</a></li> 
</ul> 
</div> 
</div> 
<script> 
// 这里引用jQuery.roll代码 
</script> 
<script> 
$(function(){ 
var roll_jQuery, contents; 
roll_jQuery = $.roll('.J_Roll_Content', '#J_Roll_Container', { effect: 'scrollX', duration: 40, delay: 500 }).rollRun(); 
contents = $('.J_Roll_Content'); 
contents.bind('mouseenter', function(){ 
roll_jQuery.rollStop(); 
}); 
contents.bind('mouseleave', function(){ 
roll_jQuery.rollRun(); 
}); 
}); 
</script> 
</body> 
</html>

例3.2
View Code 
<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<meta name="description" content="jQuery实现无间隙滚动效果"> 
<title>jQuery demo</title> 
<style> 
body { font: 12px/1.5 tahoma, "microsoft yahei", "微软雅黑E\8F6F\96C5\9ED1"; } 
body, div, ul, li, h1 { margin: 0; padding: 0; } 
.news { margin: 100px 0 0 100px; } 
.news ul { list-style: none; } 
.news-box { padding: 20px; width: 310px; height: 192px; background-color: #fcfcfd; overflow: hidden; position: relative; } 
.news h1 { margin-bottom: 15px; padding-left: 20px; color: #370188; } 
.news-list { position: absolute; } 
.news-list li { width: 100%; height: 24px; line-height: 24px; overflow: hidden; word-wrap: normal; } 
.news-list li a { text-decoration: none; color: #000; } 
.news-list li a:hover { 
-webkit-transition: color .2s linear, background-color .3s linear; 
-moz-transition: color .2s linear, background-color .3s linear; 
-ms-transition: color .2s linear, background-color .3s linear; 
-o-transition: color .2s linear, background-color .3s linear; 
transition: color .2s linear, background-color .3s linear; 
color: #FF4400; 
text-decoration: underline; 
} 
.news-list li a:visited { color: #290065; } 
</style> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
</head> 
<body> 
<div id="J_news" class="news"> 
<h1>文字列表滚动</h1> 
<div id="J_Roll_Container" class="news-box"> 
<ul class="J_Roll_Content news-list"> 
<li><a href="http://news.ifeng.com/world/detail_2013_01/11/21098447_0.shtml" target="_blank">在澳中国留学生涉嫌接送非法色情业者赚外快被罚</a></li> 
<li><a href="http://news.qq.com/a/20130111/000993.htm" target="_blank">印度北部等今冬遭遇极寒天气 已致数百人被冻死</a></li> 
<li><a href="http://news.qq.com/a/20130111/001251.htm" target="_blank">意大利警方禁止挂中国红灯笼 被指危险引燃物</a></li> 
<li><a href="http://news.sina.com.cn/c/2013-01-10/121926003955.shtml" target="_blank">日方否认将对中国巡航钓鱼岛飞机</a></li> 
<li><a href="http://news.hf365.com/system/2013/01/10/012875943.shtml" target="_blank">日本防卫省否认将对中国飞机警告射击(图)</a></li> 
<li><a href="http://news.21cn.com/world/guojisaomiao/2013/01/10/14338977.shtml" target="_blank">日否认警告射击中海监飞机 恐中日因夺岛开战</a></li> 
<li><a href="http://military.people.com.cn/n/2013/0110/c1011-20150261.html" target="_blank">传解放军举行长白山军演</a></li> 
<li><a href="http://news.21cn.com/world/guojisaomiao/2013/01/10/14335623.shtml" target="_blank">中国高新6号反潜机问世 反潜艇性能优于美军P-3C</a></li> 
<li><a href="http://opinion.huanqiu.com/opinion_world/2013-01/3467875.html" target="_blank">张建刚:2030年中国将圆海洋强国梦</a></li> 
<li><a href="http://news.hf365.com/system/2013/01/10/012873928.shtml" target="_blank">运-20现身试飞中心 可取代伊尔-76任何功能</a></li> 
<li><a href="http://mil.sohu.com/20130110/n362988784.shtml" target="_blank">东海舰队引入大批无人机 饱和攻击让日难以招架</a></li> 
</ul> 
</div> 
</div> 
<script> 
jQuery.extend({ 
roll: function(contentCls, contentParentId, configs){ 
var setTimeID, totalWidth = 0, totalHeight = 0, 
firstContent, secondContent, contents; 
(function(){ 
var singleContent, cloneContent, nodeList; 
singleContent = $(contentCls, contentParentId); 
nodeList = singleContent.children(); 
if (configs.effect === 'scrollX') { 
$.each(nodeList, function(idx, itm) { 
totalWidth += $(itm).outerWidth(true); 
}); 
singleContent.css({ 'width': totalWidth + 'px' }); 
} 
else if (configs.effect === 'scrollY') { 
$.each(nodeList, function(idx, itm) { 
totalHeight += $(itm).outerHeight(true); 
}); 
singleContent.css({ 'height': totalHeight + 'px' }); 
} 
cloneContent = singleContent.clone(); 
cloneContent.appendTo(contentParentId); 
contents = $(contentCls, contentParentId); 
firstContent = contents[0]; 
secondContent = contents[1]; 
if (configs.effect === 'scrollX') { 
$(firstContent).css({ 'left': 0 }); 
$(secondContent).css({ 'left': totalWidth + 'px' }); 
} 
else if (configs.effect === 'scrollY') { 
$(firstContent).css({ 'top': 0 }); 
$(secondContent).css({ 'top': totalHeight + 'px' }); 
} 
})() 
function cssAnimate(){ 
if (configs.effect === 'scrollX') { 
$(firstContent).css({ left: parseInt(firstContent.style.left, 10) - 1 + 'px' }); 
$(secondContent).css({ left: parseInt(secondContent.style.left, 10) - 1 + 'px' }); 
$.each(contents, function(idx, itm) { 
if (parseInt(itm.style.left,10) === -totalWidth) { 
$(itm).css({ left: totalWidth + 'px' }); 
} 
}); 
} 
else if (configs.effect === 'scrollY') { 
$(firstContent).css({ top: parseInt(firstContent.style.top, 10) - 1 + 'px' }); 
$(secondContent).css({ top: parseInt(secondContent.style.top, 10) - 1 + 'px' }); 
$.each(contents, function(idx, itm) { 
if (parseInt(itm.style.top,10) === -totalHeight) { 
$(itm).css({ top: totalHeight + 'px' }); 
} 
}); 
} 
setTimeId = setTimeout(cssAnimate, configs.duration); 
} 
function rollRun(){ 
setTimeId = setTimeout(cssAnimate, configs.delay); 
return jQuery; 
} 
function rollStop(){ 
clearTimeout(setTimeId); 
return jQuery; 
} 
return $.extend({ 
rollRun: rollRun, 
rollStop: rollStop 
}); 
} 
}); 
</script> 
<script> 
$(function(){ 
var roll_jQuery, contents; 
roll_jQuery = $.roll('.J_Roll_Content', '#J_Roll_Container', { effect: 'scrollY', duration: 40, delay: 500 }).rollRun(); 
contents = $('.J_Roll_Content'); 
contents.bind('mouseenter', function(){ 
roll_jQuery.rollStop(); 
}); 
contents.bind('mouseleave', function(){ 
roll_jQuery.rollRun(); 
}); 
}); 
</script> 
</body> 
</html>
Javascript 相关文章推荐
让iframe框架网页在任何浏览器下自动伸缩
Aug 18 Javascript
javascript的函数
Jan 31 Javascript
不用ajax实现点击文字即可编辑的方法
Dec 16 Javascript
JS中字符问题(二进制/十进制/十六进制及ASCII码之间的转换)
Nov 03 Javascript
JavaScript 入门基础知识 想学习js的朋友可以参考下
Dec 26 Javascript
常规表格多表头查询示例
Feb 21 Javascript
文本框只能输入数字的实现方法(兼容IE火狐)
Jun 25 Javascript
Js判断H5上下滑动方向及滑动到顶部和底部判断的示例代码
Nov 15 Javascript
webpack多入口多出口的实现方法
Aug 17 Javascript
vue-router的使用方法及含参数的配置方法
Nov 13 Javascript
用Vue.js在浏览器中实现裁剪图像功能
Jun 18 Javascript
vue中div禁止点击事件的实现
Apr 02 Vue.js
得到jQuery detach()后节点中的某个值实现代码
Feb 05 #Javascript
jquery zTree异步加载简单实例分享
Feb 05 #Javascript
JS打印gridview实现原理及代码
Feb 05 #Javascript
漂亮的jquery提示效果(仿腾讯弹出层)
Feb 05 #Javascript
Js 获取Gridview选中行的内容操作步骤
Feb 05 #Javascript
6款经典实用的jQuery小插件及源码(对话框/提示工具等等)
Feb 04 #Javascript
jquery插件实现鼠标经过图片右侧显示大图的效果(类似淘宝)
Feb 04 #Javascript
You might like
mongo Table类文件 获取MongoCursor(游标)的实现方法分析
2013/07/01 PHP
php输入流php://input使用示例(php发送图片流到服务器)
2013/12/25 PHP
使用PHP函数scandir排除特定目录
2014/06/12 PHP
php使用递归计算文件夹大小
2014/12/24 PHP
Laravel 加载第三方类库的方法
2018/04/20 PHP
Aster vs Newbee BO5 第一场2.19
2021/03/10 DOTA
javascript 弹出窗口中是否显示地址栏的实现代码
2011/04/14 Javascript
JavaScript判断数组是否包含指定元素的方法
2015/07/01 Javascript
浅析Bootstrap验证控件的使用
2016/06/23 Javascript
ES6新特性之数组、Math和扩展操作符用法示例
2017/04/01 Javascript
jQuery中hover方法搭配css的hover选择器,实现选中元素突出显示方法
2017/05/08 jQuery
Javascript实现倒计时时差效果
2017/05/18 Javascript
原生js的ajax和解决跨域的jsonp(实例讲解)
2017/10/16 Javascript
thinkjs 文件上传功能实例代码
2017/11/08 Javascript
JS实现模糊查询带下拉匹配效果
2018/06/21 Javascript
Vue头像处理方案小结
2018/07/26 Javascript
微信小程序BindTap快速连续点击目标页面跳转多次问题处理
2019/04/08 Javascript
微信小程序调用天气接口并且渲染在页面过程详解
2019/06/24 Javascript
uni-app使用微信小程序云函数的步骤示例
2020/05/22 Javascript
Vue解决移动端弹窗滚动穿透问题
2020/12/15 Vue.js
easy_install python包安装管理工具介绍
2013/02/10 Python
Python实现字符串与数组相互转换功能示例
2017/09/22 Python
Python输出带颜色的字符串实例
2017/10/10 Python
Python3数字求和的实例
2019/02/19 Python
在Python中os.fork()产生子进程的例子
2019/08/08 Python
使用Python获取当前工作目录和执行命令的位置
2020/03/09 Python
OpenCV 之按位运算举例解析
2020/06/19 Python
详解python变量与数据类型
2020/08/25 Python
详解python命令提示符窗口下如何运行python脚本
2020/09/11 Python
HTML5 中新的全局属性(整理)
2013/07/31 HTML / CSS
韩国著名的在线综合购物网站:Akmall
2016/08/07 全球购物
英国家喻户晓的折扣商场:TK Maxx
2017/05/26 全球购物
优秀党员转正的自我评价
2013/10/06 职场文书
诉前财产保全担保书
2014/05/20 职场文书
详解 TypeScript 枚举类型
2021/11/02 Javascript
三星 3nm 芯片将于第二季度开始量产
2022/04/29 数码科技