最近用到一個頁面上圖片左右切換和新聞列表滾動呈現的效果,整理如下:
前段代碼:
. 代碼如下:
<!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=gb2312" />
<link rel="stylesheet" href="css/css.css" type="text/css" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/kxbdSuperMarquee.js"></script>
<script type="text/javascript" src="js/indexPic.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
$('.indexNews div.core').kxbdSuperMarquee({
isEqual:false,
distance:18,
time:4,
//btnGo:{up:'#goU',down:'#goD'},
direction:'up'
});
});
</script>
</script>
<title></title>
</head>
<body>
<div class="outer">
<div class="banner">
<div class="flash_img" id="fadImgs">
<ul class="img-box" style="">
<li><a target="_blank" href="#"><img alt="首頁AD-1" src="Images/ad-1.jpg" width="1980" height="449"></a></li>
<li><a target="_blank" href="#"><img alt="首頁AD-2" src="images/ad-2.jpg" width="1980" height="449"></a></li>
<li><a target="_blank" href="#"><img alt="首頁AD-3" src="images/ad-3.jpg" width="1980" height="449"></a></li>
<li><a target="_blank" href="#"><img alt="首頁AD-4" src="Images/ad-4.jpg" width="1980" height="449"></a></li>
</ul>
</div>
</div>
<div class="indexNews">
<strong class="fl mr10"><a href="#">新聞中心</a></strong>
<div class="core">
<ul>
<li><a href="#" title="新聞1">新聞標題1</a></li>
<li><a href="#" title="新聞2">新聞標題2</a></li>
<li><a href="#" title="新聞3”">新聞標題3</a></li>
<li><a href="#" title="新聞4">新聞標題4</a></li>
<li><a href="#" title="新聞5">新聞標題5</a></li>
</ul>
</div>
</div>
</div>
<script type="text/javascript">
$(function() {
var len = $(".img-box li").length;
var imgW = $(".img-box li").width() * len;
$(".img-box").width(imgW);
$(".info_slide_dots span").hide();
})
$("#fadImgs").slideImg({
speed: "slow",
timer: 5000
});
</script>
</body>
</html>
css文件夾包含一個樣式表css.css:
. 代碼如下:
/* reset.css */
body{background:#fff;color:#444;height:100%;line-height:1.4;}
html{height:100%;overflow-y:scroll;-webkit-text-size-adjust:none;}
body,input,button,textarea,select,h1,h2,h3,h4,h5,h6{font:12px/1.5 Arial,Tahoma,Helvetica,"\5b8b\4f53",sans-serif;}
body,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,blockquote,th,td{margin:0;padding:0;}
img{vertical-align:bottom;}
/* base.css */
a:link,a:visited,a:hover,a:active{cursor:pointer;text-decoration:none;}
a:active,a:focus {-moz-outline:none;outline:none;ie-dummy:expression(this.hideFocus=true);}
a:hover {text-decoration:underline;}
li {list-style:none;}
ol li {display:inline;}
/*common.css*/
.outer{position:relative; overflow:hidden; background:url(../images/line.gif) repeat-x bottom; border-bottom:solid 1px #fff; margin:0 auto;}
.banner{position:absolute; left:50%; top:0; margin-left:-990px; height:400px; width:1980px;overflow:hidden;}
.flash_img {
overflow: hidden;
width: 1980px;
height: 400px;
position: relative
}
.banner{height:400px;}
.flash_img .img-box {
position: absolute;
}
.flash_img .img-box li{float:left;}
.flash_img img {
display: block;
}
.flash_item {
position: absolute;
right: 510px;
bottom: 10px;
height: 12px;
}
.flash_item li {
background:url(../images/flash_item.png) no-repeat right;
color: #D94B01;
cursor: pointer;
float: left;
font-size: 12px;
height: 12px;
line-height: 12px;
margin-left: 15px;
text-align: center;
width: 12px;
}
.flash_item li.on
{
background-position:left;
color: #FFFFFF;
font-weight: bold;
height: 12px;
line-height: 12px;
width: 12px;
}
.flash_item li img {
display: block
}
/*default.css*/
.indexNews{width:700px; height:29px; bottom:0; left:50%; position:absolute; margin-left:-480px; z-index:999;}
.indexNews strong{color:#409dd9;float: left;}
.indexNews strong a{color:#409dd9;text-decoration:none;}
.indexNews .core{height:18px; line-height:18px; overflow:hidden;}
.indexNews .core span{color:#409dd9; margin-right:20px;}
.indexNews .core a{margin-left:10px;color:#666;}
.outer{height:440px;}
js文件夾包含三個文件:jquery-1.7.2.min.js、indexPic.js、kxbdSuperMarquee.js。第一個可以在網上直接下載,第二個是切換圖片,第三個滾動新聞列表。
indexPic.js:
. 代碼如下:
document.writeln("<script type=\"text/javascript\" >BAIDU_CLB_SLOT_ID = \"586642\";</script>");
document.writeln("<script type=\"text/javascript\" src=\"http://cbjs.baidu.com/js/o.js\"></script>");
(function($) {
$.fn.slideImg = function(settings) {
settings = jQuery.extend({
speed: "normal",
timer: 1000
}, settings);
return this.each(function() {
$.fn.slideImg.scllor($(this), settings);
});
};
$.fn.slideImg.scllor = function($this, settings) {
var index = 0;
var scllorer = $(".img-box li", $this);
var size = scllorer.size();
var slideW = scllorer.outerWidth();
var $selItem = $("<ul></ul>", {
"class": "flash_item",
html: function() {
var $selItemHTML = "";
if (size == 1) {
return;
} else if (size > 1) {
for (var i = 0; i < size; i++) {
i == 0 ? $selItemHTML += '<li class="on">' : $selItemHTML += '<li class=""></li>';
}
return $selItemHTML;
}
}
}).appendTo($this);
var li = $(".flash_item li", $this);
var showBox = $(".img-box", $this);
var intervalTime = null;
li.hover(function() {
var that = this;
if (intervalTime) {
clearInterval(intervalTime);
}
intervalTime = setTimeout(function() {
index = li.index(that);
ShowAD(index);
}, 200);
}, function() {
clearInterval(intervalTime);
interval();
});
showBox.hover(function() {
if (intervalTime) {
clearInterval(intervalTime);
}
}, function() {
clearInterval(intervalTime);
interval();
});
function interval() {
intervalTime = setInterval(function() {
ShowAD(index);
index++;
if (index == size) {
index = 0;
}
}, settings.timer);
}
interval();
var ShowAD = function(i) {
showBox.animate({ "left": -i * slideW }, settings.speed);
li.eq(i).addClass("on").siblings().removeClass("on");
};
};
})(jQuery);
kxbdSuperMarquee.js:
. 代碼如下:
/**
* @classDescription 超級Marquee,可做圖片導航,圖片輪換
* @author Aken Li(www.kxbd.com)
* @date 2009-07-27
* @dependence jQuery 1.3.2
* @DOM
* <div id="marquee">
* <ul>
* <li></li>
* <li></li>
* </ul>
* </div>
* @CSS
* #marquee {width:200px;height:50px;overflow:hidden;}
* @Usage
* $('#marquee').kxbdSuperMarquee(options);
* @options
* distance:200,//一次滾動的距離
* duration:20,//緩動效果,單次移動時間,越小速度越快,為0時無緩動效果
* time:5,//停頓時間,單位為秒
* direction: 'left',//滾動方向,'left','right','up','down'
* scrollAmount:1,//步長
* scrollDelay:20//時長,單位為毫秒
* isEqual:true,//所有滾動的元素長寬是否相等,true,false
* loop: 0,//循環滾動次數,0時無限
* btnGo:{left:'#goL',right:'#goR'},//控制方向的按鈕ID,有四個屬性left,right,up,down分別對應四個方向
* eventGo:'click',//鼠標事件
* controlBtn:{left:'#goL',right:'#goR'},//控制加速滾動的按鈕ID,有四個屬性left,right,up,down分別對應四個方向
* newAmount:4,//加速滾動的步長
* eventA:'mouseenter',//鼠標事件,加速
* eventB:'mouseleave',//鼠標事件,原速
* navId:'#marqueeNav', //導航容器ID,導航DOM:<ul><li>1</li><li>2</li><ul>,導航CSS:.navOn
* eventNav:'click' //導航事件
*/
(function($){
$.fn.kxbdSuperMarquee = function(options){
var opts = $.extend({},$.fn.kxbdSuperMarquee.defaults, options);
return this.each(function(){
var $marquee = $(this);//滾動元素容器
var _scrollObj = $marquee.get(0);//滾動元素容器DOM
var scrollW = $marquee.width();//滾動元素容器的寬度
var scrollH = $marquee.height();//滾動元素容器的高度
var $element = $marquee.children(); //滾動元素
var $kids = $element.children();//滾動子元素
var scrollSize=0;//滾動元素尺寸
var _type = (opts.direction == 'left' || opts.direction == 'right') ? 1:0;//滾動類型,1左右,0上下
var scrollId, rollId, isMove, marqueeId;
var t,b,c,d,e; //滾動動畫的參數,t:當前時間,b:開始的位置,c:改變的位置,d:持續的時間,e:結束的位置
var _size, _len; //子元素的尺寸與個數
var $nav,$navBtns;
var arrPos = [];
var numView = 0; //當前所看子元素
var numRoll=0; //輪換的次數
var numMoved = 0;//已經移動的距離
//防止滾動子元素比滾動元素寬而取不到實際滾動子元素寬度
$element.css(_type?'width':'height',10000);
//獲取滾動元素的尺寸
var navHtml = '<ul>';
if (opts.isEqual) {
_size = $kids[_type?'outerWidth':'outerHeight']();
_len = $kids.length;
scrollSize = _size * _len;
for(var i=0;i<_len;i++){
arrPos.push(i*_size);
navHtml += '<li>'+ (i+1) +'</li>';
}
}else{
$kids.each(function(i){
arrPos.push(scrollSize);
scrollSize += $(this)[_type?'outerWidth':'outerHeight']();
navHtml += '<li>'+ (i+1) +'</li>';
});
}
navHtml += '</ul>';
//滾動元素總尺寸小於容器尺寸,不滾動
if (scrollSize<(_type?scrollW:scrollH)) return;
//克隆滾動子元素將其插入到滾動元素後,並設定滾動元素寬度
$element.append($kids.clone()).css(_type?'width':'height',scrollSize*2);
//輪換導航
if (opts.navId) {
$nav = $(opts.navId).append(navHtml).hover( stop, start );
$navBtns = $('li', $nav);
$navBtns.each(function(i){
$(this).bind(opts.eventNav,function(){
if(isMove) return;
if(numView==i) return;
rollFunc(arrPos[i]);
$navBtns.eq(numView).removeClass('navOn');
numView = i;
$(this).addClass('navOn');
});
});
$navBtns.eq(numView).addClass('navOn');
}
//設定初始位置
if (opts.direction == 'right' || opts.direction == 'down') {
_scrollObj[_type?'scrollLeft':'scrollTop'] = scrollSize;
}else{
_scrollObj[_type?'scrollLeft':'scrollTop'] = 0;
}
if(opts.isMarquee){
//滾動開始
//marqueeId = setInterval(scrollFunc, opts.scrollDelay);
marqueeId = setTimeout(scrollFunc, opts.scrollDelay);
//鼠標劃過停止滾動
$marquee.hover(
function(){
clearInterval(marqueeId);
},
function(){
//marqueeId = setInterval(scrollFunc, opts.scrollDelay);
clearInterval(marqueeId);
marqueeId = setTimeout(scrollFunc, opts.scrollDelay);
}
);
//控制加速運動
if(opts.controlBtn){
$.each(opts.controlBtn, function(i,val){
$(val).bind(opts.eventA,function(){
opts.direction = i;
opts.oldAmount = opts.scrollAmount;
opts.scrollAmount = opts.newAmount;
}).bind(opts.eventB,function(){
opts.scrollAmount = opts.oldAmount;
});
});
}
}else{
if(opts.isAuto){
//輪換開始
start();
//鼠標劃過停止輪換
$marquee.hover( stop, start );
}
//控制前後走
if(opts.btnGo){
$.each(opts.btnGo, function(i,val){
$(val).bind(opts.eventGo,function(){
if(isMove == true) return;
opts.direction = i;
rollFunc();
if (opts.isAuto) {
stop();
start();
}
});
});
}
}
function scrollFunc(){
var _dir = (opts.direction == 'left' || opts.direction == 'right') ? 'scrollLeft':'scrollTop';
if(opts.isMarquee){
if (opts.loop > 0) {
numMoved+=opts.scrollAmount;
if(numMoved>scrollSize*opts.loop){
_scrollObj[_dir] = 0;
return clearInterval(marqueeId);
}
}
var newPos = _scrollObj[_dir]+(opts.direction == 'left' || opts.direction == 'up'?1:-1)*opts.scrollAmount;
}else{
if(opts.duration){
if(t++<d){
isMove = true;
var newPos = Math.ceil(easeOutQuad(t,b,c,d));
if(t==d){
newPos = e;
}
}else{
newPos = e;
clearInterval(scrollId);
isMove = false;
return;
}
}else{
var newPos = e;
clearInterval(scrollId);
}
}
if(opts.direction == 'left' || opts.direction == 'up'){
if(newPos>=scrollSize){
newPos-=scrollSize;
}
}else{
if(newPos<=0){
newPos+=scrollSize;
}
}
_scrollObj[_dir] = newPos;
if(opts.isMarquee){
marqueeId = setTimeout(scrollFunc, opts.scrollDelay);
}else if(t<d){
if(scrollId) clearTimeout(scrollId);
scrollId = setTimeout(scrollFunc, opts.scrollDelay);
}else{
isMove = false;
}
};
function rollFunc(pPos){
isMove = true;
var _dir = (opts.direction == 'left' || opts.direction == 'right') ? 'scrollLeft':'scrollTop';
var _neg = opts.direction == 'left' || opts.direction == 'up'?1:-1;
numRoll = numRoll +_neg;
//得到當前所看元素序號並改變導航CSS
if(pPos == undefined&&opts.navId){
$navBtns.eq(numView).removeClass('navOn');
numView +=_neg;
if(numView>=_len){
numView = 0;
}else if(numView<0){
numView = _len-1;
}
$navBtns.eq(numView).addClass('navOn');
numRoll = numView;
}
var _temp = numRoll<0?scrollSize:0;
t=0;
b=_scrollObj[_dir];
//c=(pPos != undefined)?pPos:_neg*opts.distance;
e=(pPos != undefined)?pPos:_temp+(opts.distance*numRoll)%scrollSize;
if(_neg==1){
if(e>b){
c = e-b;
}else{
c = e+scrollSize -b;
}
}else{
if(e>b){
c =e-scrollSize-b;
}else{
c = e-b;
}
}
d=opts.duration;
//scrollId = setInterval(scrollFunc, opts.scrollDelay);
if(scrollId) clearTimeout(scrollId);
scrollId = setTimeout(scrollFunc, opts.scrollDelay);
}
function start(){
rollId = setInterval(function(){
rollFunc();
}, opts.time*1000);
}
function stop(){
clearInterval(rollId);
}
function easeOutQuad(t,b,c,d){
return -c *(t/=d)*(t-2) + b;
}
function easeOutQuint(t,b,c,d){
return c*((t=t/d-1)*t*t*t*t + 1) + b;
}
});
};
$.fn.kxbdSuperMarquee.defaults = {
isMarquee:false,//是否為Marquee
isEqual:true,//所有滾動的元素長寬是否相等,true,false
loop: 0,//循環滾動次數,0時無限
newAmount:3,//加速滾動的步長
eventA:'mousedown',//鼠標事件,加速
eventB:'mouseup',//鼠標事件,原速
isAuto:true,//是否自動輪換
time:5,//停頓時間,單位為秒
duration:50,//緩動效果,單次移動時間,越小速度越快,為0時無緩動效果
eventGo:'click', //鼠標事件,向前向後走
direction: 'left',//滾動方向,'left','right','up','down'
scrollAmount:1,//步長
scrollDelay:10,//時長
eventNav:'click'//導航事件
};
$.fn.kxbdSuperMarquee.setDefaults = function(settings) {
$.extend( $.fn.kxbdSuperMarquee.defaults, settings );
};
})(jQuery);
images文件夾用的圖標: