在企业站中,我们会看到很多新闻列表很平滑的滚动,但是这种功能自己写太浪费时间,下面是我整理好的一组很常用的新闻列表滚动,有上下分页哦!

1.body里面

 <div class="tz_tagcgnewcontent">
<div id="feature-slide-block">
<div class="tz_newlist">
<div class="tz_newimg"><img src="data:images/tz_img03.jpg" /></div>
<div class="tz_newcontent">
<!--新闻标题只显示2行 截取字符串-->
<div class="tz_newtitle">为什么说腾讯的兴趣社交必然失败1?</div>
<!--新闻内容只显示3行 截取字符串-->
<div class="tz_newdes">腾讯基于强联系的社交关系很难能成为兴趣社交的基石,或许这次尝试将会成为腾讯在社交领域继腾讯微博之后的又一次失败。</div>
</div>
</div>
<div class="tz_newlist" style="display: none; ">
<div class="tz_newimg"><img src="data:images/tz_img03.jpg" /></div>
<div class="tz_newcontent">
<!--新闻标题只显示2行 截取字符串-->
<div class="tz_newtitle">为什么说腾讯的兴趣社交必然失败2?</div>
<!--新闻内容只显示3行 截取字符串-->
<div class="tz_newdes">腾讯基于强联系的社交关系很难能成为兴趣社交的基石,或许这次尝试将会成为腾讯在社交领域继腾讯微博之后的又一次失败。</div>
</div>
</div>
<div class="tz_newlist" style="display: none; ">
<div class="tz_newimg"><img src="data:images/tz_img03.jpg" /></div>
<div class="tz_newcontent">
<!--新闻标题只显示2行 截取字符串-->
<div class="tz_newtitle">为什么说腾讯的兴趣社交必然失败3?</div>
<!--新闻内容只显示3行 截取字符串-->
<div class="tz_newdes">腾讯基于强联系的社交关系很难能成为兴趣社交的基石,或许这次尝试将会成为腾讯在社交领域继腾讯微博之后的又一次失败。</div>
</div>
</div>
<div class="tz_newlist" style="display: none; ">
<div class="tz_newimg"><img src="data:images/tz_img03.jpg" /></div>
<div class="tz_newcontent">
<!--新闻标题只显示2行 截取字符串-->
<div class="tz_newtitle">为什么说腾讯的兴趣社交必然失败4?</div>
<!--新闻内容只显示3行 截取字符串-->
<div class="tz_newdes">腾讯基于强联系的社交关系很难能成为兴趣社交的基石,或许这次尝试将会成为腾讯在社交领域继腾讯微博之后的又一次失败。</div>
</div>
</div>
<div id="feature-slide-list" class="tz_newpage"> <a href="#" id="feature-slide-list-previous" class="tz_newpre"></a>
<div id="feature-slide-list-items" class="tz_newpg"> </div>
<a href="#" id="feature-slide-list-next" class="tz_newnext"></a> </div>
</div>
<script type="text/javascript">
initFeatureSlide();
</script>
</div>

2.样式

 .tz_tagcgnewcontent{ width:680px; height:230px; overflow:hidden; position:relative;}
.tz_newlist{ width:680px; height:230px; overflow:hidden; float:left;}
.tz_newimg{ width:380px; height:228px; overflow:hidden; float:left; margin:10px 20px 0 0;}
.tz_newimg img{ width:380px; height:228px; overflow:hidden;}
.tz_newcontent{ width:270px; height:228px; overflow:hidden; float:left; margin:10px 0 0 0;}
.tz_newtitle{ width:100%; height:84px; overflow:hidden; line-height:34px; border-bottom:1px dashed #d2d0d1; font-size:24px; color:#333;}
.tz_newdes{ width:100%; height:68px; overflow:hidden; line-height:23px; color:#666; margin-top:16px;}
.tz_newpage{ width:100px; height:17px; overflow:hidden; position:absolute; bottom:10px; right:100px;}
.tz_newpre{ width:17px; height:17px; overflow:hidden; float:left; background:url(../images/tz_bj04.png) no-repeat;}
.tz_newpre:hover{ background:url(../images/tz_bj04.png) no-repeat -34px 0;}
.tz_newpg{ width:56px; height:7px; overflow:hidden; margin:5px 0 0 10px; float:left;}
.tz_newpg a{ width:7px; height:7px; overflow:hidden; display:block; float:left; margin-right:5px; background:url(../images/tz_bj03.png) no-repeat;}
.tz_newpg a:hover{ background:url(../images/tz_bj03.png) no-repeat -7px 0;}
.tz_newpg a.current{ background:url(../images/tz_bj03.png) no-repeat -7px 0;}
.tz_newnext{ width:17px; height:17px; overflow:hidden; float:right; background:url(../images/tz_bj04.png) no-repeat -17px 0;}
.tz_newnext:hover{ background:url(../images/tz_bj04.png) no-repeat -51px 0;}

3.javascript

 function initFeatureSlide(strId) {
var domRoot = document.getElementById('feature-slide-block');
if (!domRoot) return;
domRoot.list = [];
var children = domRoot.childNodes;
var offset = 0;
for (var i in children) {
var domItem = children[i];
if (domItem && domItem.className == 'tz_newlist') {
domRoot.list.push(domItem);
domItem.offset = offset;
offset++;
}
}
var domList = document.getElementById('feature-slide-list-items');
if (!domList) return;
domList.innerHTML = '';
domList.items = [];
for (var i = 0; i < domRoot.list.length; i++) {
var temp = domRoot.list[i];
var domItem = document.createElement('a');
domList.appendChild(domItem);
domItem.href = '#';
domItem.onclick = function(){
return false;
}
domList.items.push(domItem);
domItem.offset = i;
}
var domPreviousBtn = document.getElementById('feature-slide-list-previous');
var domNextBtn = document.getElementById('feature-slide-list-next');
domPreviousBtn.onclick = function(evt) {
evt = evt || window.event;
var target = evt.target || evt.srcElement;
var offset = domList.current.offset;
offset--;
if (offset >= domList.items.length || offset < 0)
offset = domList.items.length - 1;
target.blur();
doSlide(offset);
return false;
}
domNextBtn.onclick = function(evt) {
evt = evt || window.event;
var target = evt.target || evt.srcElement;
var offset = domList.current.offset;
offset++;
if (offset >= domList.items.length || offset < 0)
offset = 0;
target.blur();
doSlide(offset);
return false;
}
domRoot.current = domRoot.list[0];
domList.current = domList.items[0];
domRoot.current.style.display = 'block';
domList.current.className = 'current';
function doSlide(offset, timeStamp) {
if (
timeStamp &&
(
domRoot.boolHover ||
timeStamp != domRoot.timeStamp
)
) return; if (typeof(offset) != 'number') {
offset = domList.current.offset - (-1);
if (offset >= domList.items.length || offset < 0)
offset = 0;
}
domRoot.current.style.display = 'none';
domList.current.className = '';
domRoot.current = domRoot.list[offset];
domList.current = domList.items[offset];
domRoot.current.style.display = 'block';
domList.current.className = 'current';
if (domRoot.boolHover) return;
var now = new Date();
domRoot.timeStamp = now.valueOf();
window.setTimeout(function() {
doSlide(null, now.valueOf());
}, 5000);
}
domList.onmouseover = domList.onclick = function (evt) {
evt = evt || window.event;
var target = evt.target || evt.srcElement;
while (target && target != domList) {
if (target.tagName.toLowerCase() == 'a') {
target.blur();
doSlide(target.offset);
return false;
}
target = target.parentNode;
}
}
domRoot.onmouseover = domRoot.onmousemove = function() {
domRoot.boolHover = true;
}
domRoot.onmouseout = function(evt) {
domRoot.boolHover = false;
var now = new Date();
domRoot.timeStamp = now.valueOf();
window.setTimeout(function() {
doSlide(null, now.valueOf());
}, 5000);
}
var now = new Date();
domRoot.timeStamp = now.valueOf();
window.setTimeout(function() {
doSlide(null, now.valueOf());
}, 5000);
}

这段代码能用,但是可以优化的地方还有很多,大家可以提提意见

javascript写的新闻滚动代码的更多相关文章

  1. JavaScript写倒计时

    在网页中,特别是电商网站中,倒计时的出现频率很高,接下来给大家介绍一下怎么用JavaScript写一个倒计时.代码如下: 首先我们通过Date构造函数的方法创建一个倒计时的结束的时间.并将其转换为毫秒 ...

  2. html+css+javascript实现列表循环滚动示例代码

    使用html+css+javascript实现列表循环滚动,设置时间定时,在规定的时间内替换前一个节点的内容,具体示例如下,感兴趣的朋友可以参考下 说明:设置时间定时,在规定的时间内替换前一个节点的内 ...

  3. 多行滚动jQuery循环新闻列表代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 使用JavaScript实现新闻滚动效果

    最近要实现一个滚动新闻效果,在网上查了一些资料,发现基本的实现方法有两种: 使用Marquee标签.这个标签的使用我已经转载了一篇比较详细的文章,这个标签的优点是便于使用,缺点是人们已经逐渐不适用它了 ...

  5. javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数

    javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数 function test(){ var bt = document.getElementById(" ...

  6. HTML滚动字幕代码参数详解及Js间隔滚动代码

    html文字滚动代码 <marquee style="WIDTH: 388px; HEIGHT: 200px" scrollamount="2" dire ...

  7. 原生javascript写的侧栏跟随效果

    浏览网站时经常看到有的网站上,当一个页面很长的时候,设定侧栏内容会跟随滚动条滚动,我们把这种效果叫做“侧栏跟随滚动”.这种特效对提高网站浏览量.文章点击率.广告点击量都有一定效果. 侧栏跟随滚动的实现 ...

  8. 微信小程序调用后台接口+热点新闻滚动展示

    1.微信JS文件,发送请求调用:  //将返回接口数据,写入Page({data})里面 //获取热点新闻,这个也是写在onload:function(){//code)里面的 wx.request( ...

  9. Js上下左右无缝隙滚动代码

    转载:http://www.cnblogs.com/chenjt/p/4193464.html 主要用到dom.offsetWidth 这个表示实际的宽度. dom.scrollLeft 这个表示这个 ...

随机推荐

  1. 搭建phonegap开发环境,搭建安卓开发环境

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. 织梦dedecms5.7后台进去就卡死解决方法

    症状:进入dede后台点击菜单后,浏览器进入假死状态要等好久才能反应过来. 解决方式:1.打开后台目录dede/templets/ 2.找到index_body.htm文件中的第25行至第35行部分屏 ...

  3. Cut the sticks

    def main(): n = int(raw_input()) arr = map(int, raw_input().strip().split()) for i in range(n): cutN ...

  4. 关于svcutil.exe

    添加环境变量 name NETFX4 value C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools Server.Pr ...

  5. LDD命令--可执行文件依赖的库出现错误时

    http://littlepig3056.blog.163.com/blog/static/180758353201212751814134/ ldd  查看可执行文件依赖的库,结果会列出依赖的库名及 ...

  6. hdu 1142 A Walk Through the Forest

    http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...

  7. Intel 凌动 D525 产品参数Intel 凌动 Z3735F 产品参数

    https://item.taobao.com/item.htm?spm=a230r.1.14.8.kauehT&id=40450541158&ns=1&abbucket=19 ...

  8. shell下有操作json对象的库

    http://kernelpanic.im/blog/2012/03/08/shell-manipulate-json/ Json.org推荐了两个:Jshon和JSON.sh 其中JSON.sh是完 ...

  9. 【转】兼容性测试套件(CTS)框架用户手册

    原文网址:http://blog.sina.com.cn/s/blog_416166e90102v6bi.html 兼容性测试套件(CTS)框架用户手册 1.为什么需要兼容性测试(以下称CTS)? 2 ...

  10. C#优秀开源资料收集

    1. 把对命令行程序的调用封装起来,通过程序里进行输入,调用命令行程序的输出显示在程序中 http://www.codeproject.com/Articles/335909/Embedding-a- ...