javascript写的新闻滚动代码
在企业站中,我们会看到很多新闻列表很平滑的滚动,但是这种功能自己写太浪费时间,下面是我整理好的一组很常用的新闻列表滚动,有上下分页哦!
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写的新闻滚动代码的更多相关文章
- JavaScript写倒计时
在网页中,特别是电商网站中,倒计时的出现频率很高,接下来给大家介绍一下怎么用JavaScript写一个倒计时.代码如下: 首先我们通过Date构造函数的方法创建一个倒计时的结束的时间.并将其转换为毫秒 ...
- html+css+javascript实现列表循环滚动示例代码
使用html+css+javascript实现列表循环滚动,设置时间定时,在规定的时间内替换前一个节点的内容,具体示例如下,感兴趣的朋友可以参考下 说明:设置时间定时,在规定的时间内替换前一个节点的内 ...
- 多行滚动jQuery循环新闻列表代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 使用JavaScript实现新闻滚动效果
最近要实现一个滚动新闻效果,在网上查了一些资料,发现基本的实现方法有两种: 使用Marquee标签.这个标签的使用我已经转载了一篇比较详细的文章,这个标签的优点是便于使用,缺点是人们已经逐渐不适用它了 ...
- javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数
javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数 function test(){ var bt = document.getElementById(" ...
- HTML滚动字幕代码参数详解及Js间隔滚动代码
html文字滚动代码 <marquee style="WIDTH: 388px; HEIGHT: 200px" scrollamount="2" dire ...
- 原生javascript写的侧栏跟随效果
浏览网站时经常看到有的网站上,当一个页面很长的时候,设定侧栏内容会跟随滚动条滚动,我们把这种效果叫做“侧栏跟随滚动”.这种特效对提高网站浏览量.文章点击率.广告点击量都有一定效果. 侧栏跟随滚动的实现 ...
- 微信小程序调用后台接口+热点新闻滚动展示
1.微信JS文件,发送请求调用: //将返回接口数据,写入Page({data})里面 //获取热点新闻,这个也是写在onload:function(){//code)里面的 wx.request( ...
- Js上下左右无缝隙滚动代码
转载:http://www.cnblogs.com/chenjt/p/4193464.html 主要用到dom.offsetWidth 这个表示实际的宽度. dom.scrollLeft 这个表示这个 ...
随机推荐
- worklight 中添加时间控件
在我们使用worklight开发的过程中,由于文档的不开源和插件的缺少,总是自己琢磨很多东东,更有胜者 需要调用源代码实现某些不易实现的功能.在这里把实现的功能代码贴出来,如有不足之处还望指正! 实现 ...
- CSS的权重(转)
CSS写的渐渐多了,他的权重问题就不得不昂首面对,之前一直得过且过的将就用着,直到最近遇到了几个大坑,一直割刺着我对前端的热情,得了得了,蒙不过去了,就发点时间记下来吧,当然还是一片转载的文章,有时候 ...
- 基于cygwin构建u-boot(五)结尾:shell 工具
结尾,基于cygwin对u-boot的处理,很大一部分都是再处理 路径等相关的问题,只有一个涉及到gcc的参数配置. 为了达到顺利编译的目的,使用shell下的部分工具进行处理. 1.sed sed简 ...
- IOS开发笔记(4)数据离线缓存与读取
IOS开发笔记(4)数据离线缓存与读取 分类: IOS学习2012-12-06 16:30 7082人阅读 评论(0) 收藏 举报 iosiOSIOS 方法一:一般将服务器第一次返回的数据保存在沙盒里 ...
- !!!!OpenWrt系列教程汇总
OpenWrt FAQ https://dev.openwrt.org.cn/wiki/faqs OpenWrt编译教程 完全新手教程:openwrt编译全过程(sse) 直接编译出带中文的openw ...
- hdu 5429 Geometric Progression(存个大数模板)
Problem Description Determine whether a sequence is a Geometric progression or not. In mathematics, ...
- boost格式化输出xml
我的boost为1.56而不是1.55 boost在xml的例子给出了一段写xml文件的代码,我简化如下: void debug_settings::save(const std::string &a ...
- python3-day3(深浅copy)
1.对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. import copy n1 = 123 print(id(n1)) n2 = n1 print(id(n ...
- UVA 673 (13.08.17)
Parentheses Balance You are given a string consisting of parentheses () and []. Astring of this ty ...
- vue 单页面应用实战
1. 为什么要 SPA? SPA: 就是俗称的单页应用(Single Page Web Application). 在移动端,特别是 hybrid 方式的H5应用中,性能问题一直是痛点. 使用 SPA ...