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

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. 使用ES6进行开发的思考

    ECMAScript6已经于近日进入了RC阶段,而早在其处于社区讨论时,我就开始一直在尝试使用ES6进行开发的方案.在Babel推出后,基于ES6的开发也有了具体可执行的解决方案,无论是Build还是 ...

  2. 保存BASE64编码图片

    1.前端上传用户图片时,一些K数较小图片,头像图标等 .以bass64编码后的字符串传到服务器. 2.服务器接收并保留到本地. // 页面上点击保存 $.post('/imgupload/save', ...

  3. IOS APP配置.plist汇总(转自coolweather )

    IOS APP配置.plist汇总(转自coolweather ) 此文转自http://www.cocoachina.com/bbs/read.php?tid=89684&page=1 作者 ...

  4. 自学Python的点滴

    1.第一天 注释 ——任何在#符号右面的内容都是注释. 注释主要作为提供给程序读者的笔记. 程序应该包含这两行 #!/user/bin/python #Filename:**.py 2.在程序中打开P ...

  5. DJANGO用户名认证一例

    现在实例了用户登陆,就自带的功能.. urls.py ~~~~~~~~~~ (r'^login/$', login), (r'^logout/$',logout,{'next_page':'/logi ...

  6. 《Programming WPF》翻译 第9章 5.默认可视化

    原文:<Programming WPF>翻译 第9章 5.默认可视化 虽然为控件提供一个自定义外观的能力是有用的,开发者应该能够使用一个控件而不用必须提供自定义可视化.这个控件应该正好工作 ...

  7. Hessian和Burlap入门教程

    一.简介 Hessian和Burlap是由Caucho Technology提供的基于HTTP协议的轻量级远程服务解决方案.他们都致力于借助尽可能简单那的API和通信协议来简化Web服务.    He ...

  8. JavaScript 基础二

    JavaScript 事件处理程序就是一组语句,在事件(如点击鼠标或移动鼠标等)发生时执行 ●当事件之间互相影响时,需要有个先后顺序,这时我们声明一个Bool值来做约束 浏览对象: window 对象 ...

  9. cocos2d-x3.2中怎样优化Cocos2d-X游戏的内存

    在游戏项目优化中都会碰到一个问题,怎样既能降低内存又能尽量降低包的大小?在实际项目中有些经验分享一下,其实2D游戏中最占内存的就是图片资源,一张图片使用不同的纹理格式带来的性能差异巨大.下表是我在IO ...

  10. Swiper滑动Html5手机浏览器自适应

    手机网页能通过window.screen.height, width获取屏幕分辨率,于是能够通过分辨率比率来计算高度. window.onload=function(){ var swiper = d ...