以下是 jQuery新闻图片上下滚动切换代码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery新闻图片上下滚动切换代码</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/lbnews.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#scrollDiv").Scroll({line:3,speed:600,timer:6000,up:"but_up",down:"but_down"});
});
</script>
</head>
<style>
/*新闻*/
.scrollbox{width:1120px; position: relative;}
#scrollDiv{width:1120px;height:110px; overflow:hidden; display: block;}
#scrollDiv ul{ width: 1131px;}
#scrollDiv li{width:344px;height:88px;padding:10px;margin-right:11px; float:left;background: #fff;transition: 0.5s all; -webkit-transition: 0.5s all; -moz-transition: 0.5s all; -o-transition: 0.5s all;border:1px solid #dedede;}
#scrollDiv li img{width:148px;height:88px;margin-right:10px;float:left;}
#scrollDiv li span{ float: right;width:186px;height:88px; overflow: hidden; display: block;}
#scrollDiv li span h4{font-size: 14px;font-weight: bold;line-height: 14px;margin-bottom:5px;color: #a57132;}
#scrollDiv li span p{line-height: 20px;height:40px;font-size: 12px;color: #666666;display: block;overflow: hidden;}
#scrollDiv li span em{ height: 18px;line-height: 18px;text-align:center;font-size: 14px;width:30px;border:1px solid #72ae2d; display: block;color: #72ae2d;margin-top: 8px;}
#scrollDiv li:hover{ background: #7cba27;border:1px solid #72ae2d;}
#scrollDiv li:hover span h4{ color: #fff;}
#scrollDiv li:hover span p{ color: #fff;}
#scrollDiv li:hover span em{ color: #fff;border:1px solid #fff; }
.scroltit{ position: absolute; right:-30px; top:30px;width:18px;}
.scroltit small{float:right; font-size:12px;width:18px;height:18px; line-height:18px; text-align: center;background:#bbbbbb;color: #fff;font-family: "宋体"; margin:2px 0; transition: 0.5s all; -webkit-transition: 0.5s all; -moz-transition: 0.5s all; -o-transition: 0.5s all;}
.scroltit small img{margin-top:6px;}
.scroltit small:hover{ background: #ff9a22;}
/*新闻结束*/
</style>
<body>
<div class="wrap">
<div class="scrollbox cl">
<div class="scroltit"><small id="but_up"><img src="images/icon6.png"></small><small id="but_down"><img src="images/icon6_1.png"></small></div>
<div id="scrollDiv">
<ul>
<li><a href='#'><img src="images/innew.jpg" width="148" height="90"><span><h4>1一周精彩 关键词热搜</h4><p>2014,行业变革如雷霆万钧2014历经风雨日臻成熟。回眸2014,追忆那些感动的瞬间</p><em>→</em></span></a></li>
<li><a href='#'><img src="images/innew.jpg" width="148" height="90"><span><h4>2一周精彩 关键词热搜</h4><p>2014,行业变革如雷霆万钧2014历经风雨日臻成熟。回眸2014,追忆那些感动的瞬间</p><em>→</em></span></a></li>
<li><a href='#'><img src="images/innew.jpg" width="148" height="90"><span><h4>3一周精彩 关键词热搜</h4><p>2014,行业变革如雷霆万钧2014历经风雨日臻成熟。回眸2014,追忆那些感动的瞬间</p><em>→</em></span></a></li>
<li><a href='#'><img src="images/innew.jpg" width="148" height="90"><span><h4>4一周精彩 关键词热搜</h4><p>2014,行业变革如雷霆万钧2014历经风雨日臻成熟。回眸2014,追忆那些感动的瞬间</p><em>→</em></span></a></li>
<li><a href='#'><img src="images/innew.jpg" width="148" height="90"><span><h4>5一周精彩 关键词热搜</h4><p>2014,行业变革如雷霆万钧2014历经风雨日臻成熟。回眸2014,追忆那些感动的瞬间</p><em>→</em></span></a></li>
<li><a href='#'><img src="images/innew.jpg" width="148" height="90"><span><h4>6一周精彩 关键词热搜</h4><p>2014,行业变革如雷霆万钧2014历经风雨日臻成熟。回眸2014,追忆那些感动的瞬间</p><em>→</em></span></a></li>
<li><a href='#'><img src="images/innew.jpg" width="148" height="90"><span><h4>7一周精彩 关键词热搜</h4><p>2014,行业变革如雷霆万钧2014历经风雨日臻成熟。回眸2014,追忆那些感动的瞬间</p><em>→</em></span></a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
JS代码(lbnews.js):
(function($){
$.fn.extend({
Scroll:function(opt,callback){
//参数初始化 if(!opt) var opt={
}
;
var _btnUp = $("#"+ opt.up);
//Shawphy:向上按钮 var _btnDown = $("#"+ opt.down);
//Shawphy:向下按钮 var timerID;
var _this=this.eq(0).find("ul:first");
var lineH=_this.find("li:first").height() + 20,//获取行高 line=opt.line?parseInt(opt.line,10):parseInt(this.height()/lineH,10),//每次滚动的行数,默认为一屏,即父容器高度 speed=opt.speed?parseInt(opt.speed,10):500;
//卷动速度,数值越大,速度越慢(毫秒) timer=opt.timer //?parseInt(opt.timer,10):3000;
//滚动的时间间隔(毫秒) if(line==0) line=1;
var upHeight=0-line*lineH/3;
//滚动函数 var scrollUp=function(){
_btnUp.unbind("click",scrollUp);
//Shawphy:取消向上按钮的函数绑定 _this.animate({
marginTop:upHeight}
,speed,function(){
for(i=1;
i<=line;
i++){
_this.find("li:first").appendTo(_this);
}
_this.css({
marginTop:0}
);
_btnUp.bind("click",scrollUp);
//Shawphy:绑定向上按钮的点击事件}
);
}
//Shawphy:向下翻页函数 var scrollDown=function(){
_btnDown.unbind("click",scrollDown);
for(i=1;
i<=line;
i++){
_this.find("li:last").show().prependTo(_this);
}
_this.css({
marginTop:upHeight}
);
_this.animate({
marginTop:0}
,speed,function(){
_btnDown.bind("click",scrollDown);
}
);
}
//Shawphy:自动播放 var autoPlay = function(){
if(timer)timerID = window.setInterval(scrollUp,timer);
}
;
var autoStop = function(){
if(timer)window.clearInterval(timerID);
}
;
//鼠标事件绑定 _this.hover(autoStop,autoPlay).mouseout();
_btnUp.css("cursor","pointer").click( scrollUp ).hover(autoStop,autoPlay);
//Shawphy:向上向下鼠标事件绑定 _btnDown.css("cursor","pointer").click( scrollDown ).hover(autoStop,autoPlay);
}
}
)}
)(jQuery);
CSS代码(style.css):
*{font-family:"微软雅黑"}
body{background-color:#fff;width:100%;}
body,input,button,select,textarea{font-size:12px;color:#333;}
textarea{resize:none;}
body,ul,ol,li,dl,dd,p,h1,h2,h3,h4,h5,h6,form,img,fieldset,.pr,.pc{margin:0;padding:0;}
table{empty-cells:show;border-collapse:collapse;}
caption,th{text-align:left;font-weight:400;}
ul li,.xl li{list-style:none;}
h1,h2,h3,h4,h5,h6{font-size:1em;font-weight:normal;}
em,cite,i{font-style:normal;display:block;}
a{color:#666;text-decoration:none;transition:0.5s all;-webkit-transition:0.5s all;-moz-transition:0.5s all;-o-transition:0.5s all;}
a:hover{color:#cda484}
img{border:0;}
label{cursor:pointer;}
input:disabled{background-color:#fff;color:#666;}
textarea:disabled{background-color:#fff;color:#666;}
.cl:after{content:".";display:block;height:0;clear:both;visibility:hidden;!}
.cl{zoom:1;}
/*框架*/
.wrap{width:1120px;margin:0 auto;}