今天学写了一个基于jQuery焦点图切换插件,有不对的地方还请多多指教,不多说下面是代码:

1、引jQuery库

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

2、Html部分

<!--Focus Html-->
<div class="s_focus">
<ul class="f_img">
<li style="background-image:url(http://www.cnhippo.com/images/new_room.jpg);"><a href="#"></a></li>
<li style="background-image:url(http://www.cnhippo.com/images/christmas_day.jpg);"><a href="#"></a></li>
<li style="background-image:url(http://www.cnhippo.com/images/reg_banner.jpg);"><a href="#"></a></li>
</ul>
</div>

3、CSS部分

<style>
/*Test - Can be deleted*/
* { margin:0; padding:0; list-style:none;}
body { padding-top:50px;} /*CSS*/
.s_focus { position:relative; z-index:1; width:100%; min-width:940px; height:350px; padding:1px 0; border:1px solid #d9d9d9; border-width:1px 0; overflow:hidden;}
.s_focus .f_img { display:block; position:relative; z-index:1; width:100%; height:100%;}
.s_focus .f_dot { display:block; position:absolute; z-index:3; left:50%; bottom:15px; width:100px; height:10px; overflow:hidden;}
.s_focus .f_d_bg { position:absolute; z-index:2; width:100%; height:40px; left:0; bottom:1px; _bottom:0; background:#FFF; filter:alpha(opacity=30); opacity:0.3; box-shadow:0 -1px 3px #999;}
.f_img li { display:none; position:absolute; z-index:2; top:0; left:0; width:100%; height:100%; background:#fff no-repeat center 0;}
.f_img li.on { display:block;}
.f_img li a { display:block; width:100%; height:100%;}
.f_dot a { float:left; display:block; width:10px; height:100%; margin-right:10px; border-radius:50%; background:#ccc; filter:alpha(opacity=30); opacity:1; overflow:hidden;}
.f_dot a.on,
.f_dot a:hover { background:#09f;}
.s_focus .f_prev,
.s_focus .f_next { display:none; position:absolute; top:50%; filter:alpha(opacity=15); opacity:0.15; width:36px; height:70px; margin-top:-35px; background:#000; z-index:5; color:#fff; text-decoration:none; line-height:70px; text-align:center; font-family:"宋体"; font-size:18px;}
.s_focus .f_prev { left:50px;}
.s_focus .f_next { right:50px;}
.s_focus .f_next:hover,
.s_focus .f_prev:hover { filter:alpha(opacity=25); opacity:0.25;}
</style>

4、JS插件部分

<script>
/***
* Copyright (c) 2015 http://www.cnblogs.com/cymmint/
* Ver: SFocus() 0.1
* Date: 2015-01-05
* Author: cymmint
* Function: SFocus是基本jQuery的banner(焦点图)切换插件,可以通过参数配置Box宽高,
* 点的大小、位置,是否显示Prev,Next,Dot以及Dot背景条,
* 首次显示等几张banner等
*/
(function($){ $.fn.SFocus = function(opts){ var defaults = {
oBoxWidth : "100%", //Silde width
oBoxHeight: 350, //Silde heigh
isOpenDBg : false, //open dot background bar
isOpenDot : true, //open do
isOpenPN : true, //open prev/next
dotWidth : 10, //dot width
dotHeight : 10, //dot heigh
dotLeft : "50%", //dot left offse
dotBottom : 16, //dot bottom offse
time : 3500, //cut time interval
onset : 0, //Silde focus img onset index
cutEvent : "mouseenter" //Slide dot cut even }; var opts = $.extend(defaults, opts); return this.each(function(){
var obox = $(this),
img = obox.find("ul.f_img li"),
sum = img.length,
cur_id = opts.onset >= sum ? 0 : opts.onset,
new_id = cur_id == sum-1 ? 0 : cur_id+1,
T = 0; //Html init
init(obox, sum, cur_id); //Dot cut
if(opts.isOpenDot) {
var dot = obox.find("div.f_dot a"); dot.on(opts.cutEvent, function(){
clearInterval(T); if ($(this).hasClass("on")) {
return false;
}
cur_id = img.parent().find(".on").index();
new_id = $(this).index(); cut(cur_id, new_id, img, dot);
});
} //Prev&Next cut
if(opts.isOpenPN) {
var prev = obox.find("a.f_prev"),
next = obox.find("a.f_next"); prev.on("click", function(){
clearInterval(T);
cur_id = img.parent().find(".on").index();
new_id = cur_id - 1;
new_id = new_id < 0 ? sum-1 : new_id; cut(cur_id, new_id, img, dot);
}); next.on("click", function(){
clearInterval(T);
cur_id = img.parent().find(".on").index();
new_id = cur_id + 1;
new_id = new_id >= sum ? 0 : new_id; cut(cur_id, new_id, img, dot);
});
} obox.hover(function(){
clearInterval(T);
if(opts.isOpenPN) {
prev.show();
next.show();
}
}, function(){
T = setInterval(function(){ auto(img, dot, sum);}, opts.time);
if(opts.isOpenPN) {
prev.hide();
next.hide();
}
}); //Slide run auto
T = setInterval(function(){ auto(img, dot, sum);}, opts.time);
}); //Slide run auto
function auto(img, dot, sum){
var cur_id = img.parent().find(".on").index(),
new_id = cur_id + 1;
new_id = new_id >= sum ? 0 : new_id; cut(cur_id, new_id, img, dot);
} //Slide cut
function cut(cur_id, new_id, img, dot){
if(opts.isOpenDot) {
dot.removeClass("on").eq(new_id).addClass("on");
} img.eq(cur_id).stop(true, false).fadeOut(300);
img.eq(new_id).stop(true, true).fadeIn(300);
img.removeClass("on").eq(new_id).addClass("on");
} //Html init
function init(obox, sum, cur_id){
var htm = "",
dot,
img; if(opts.isOpenDBg) {
htm = '<div class="f_d_bg"></div>';
} if(opts.isOpenDot) {
htm += '<div class="f_dot">';
for(var i=0; i<sum; i++) {
htm += i == cur_id ? '<a class="on" href="javascript:;"></a>' : '<a href="javascript:;"></a>';
}
htm += '</div>';
} if(opts.isOpenPN) {
htm += '<a class="f_prev" href="javascript:;">&lt;</a><a class="f_next" href="javascript:;">&gt;</a>';
} obox.append(htm);
img = obox.find("ul.f_img");
dot = obox.find("div.f_dot"); if (!$.support.leadingWhitespace) {
obox.find("a").attr("hidefocus", true);
}
obox.css({"width":opts.oBoxWidth, "height":opts.oBoxHeight});
img.children().removeClass("on").eq(cur_id).addClass("on")
dot.children().css({"width":opts.dotWidth, "height":opts.dotHeight});
dot.css({"width":dot.children().eq(0).outerWidth(true) * sum, "height":opts.dotHeight, "left":opts.dotLeft, "bottom":opts.dotBottom, "marginLeft": -(dot.width()-10)/2});
}
} })(jQuery);
</script>

5、引用插件

<script>
$(function(){
$("div.s_focus").SFocus({
oBoxWidth : 500,
oBoxHeight: 300
});
});
</script>

6、全部代码

这是最新修改后的代码,上面的就不改了

<!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>Focus</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> </head> <body> <!--Focus Html-->
<div class="s_focus">
<ul class="f_img">
<li style="background-image:url(http://www.cnhippo.com/images/new_room.jpg);"><a href="#"></a></li>
<li style="background-image:url(http://www.cnhippo.com/images/christmas_day.jpg);"><a href="#"></a></li>
<li style="background-image:url(http://www.cnhippo.com/images/reg_banner.jpg);"><a href="#"></a></li>
</ul>
</div> <style>
/*Test - Can be deleted*/
* { margin:0; padding:0; list-style:none; outline:none;}
body { padding-top:50px;} /*CSS*/
.s_focus { position:relative; z-index:1; width:100%; min-width:940px; height:350px; padding:1px 0; border:1px solid #d9d9d9; border-width:1px 0; overflow:hidden;}
.s_focus .f_img { display:block; position:relative; z-index:1; width:100%; height:100%;}
.s_focus .f_dot { display:block; position:absolute; z-index:3; left:50%; bottom:15px; width:100px; height:10px; overflow:hidden;}
.s_focus .f_d_bg { position:absolute; z-index:2; width:100%; height:40px; left:0; bottom:1px; _bottom:0; background:#FFF; filter:alpha(opacity=30); opacity:0.3; box-shadow:0 -1px 3px #999;}
.f_img li { display:none; position:absolute; z-index:1; top:0; left:0; width:100%; height:100%; background:#fff no-repeat center 0;}
.f_img li a { display:block; width:100%; height:100%;}
.f_dot a { float:left; display:block; width:10px; height:100%; margin-right:10px; border-radius:50%; background:#ccc; overflow:hidden;}
.f_dot a.on,
.f_dot a:hover { background:#09f;}
.s_focus .f_prev,
.s_focus .f_next { display:none; position:absolute; top:50%; filter:alpha(opacity=15); opacity:0.15; width:36px; height:70px; margin-top:-35px; background:#000; z-index:5; color:#fff; text-decoration:none; line-height:70px; text-align:center; font-family:"宋体"; font-size:18px;}
.s_focus .f_prev { left:50px;}
.s_focus .f_next { right:50px;}
.s_focus .f_next:hover,
.s_focus .f_prev:hover { filter:alpha(opacity=25); opacity:0.25;}
</style> <script>
$(function(){
$("div.s_focus").SFocus({
oBoxWidth : 500,
oBoxHeight: 300,
onset : 2
});
});
</script> <script>
/***
* Copyright (c) 2015 http://www.cnblogs.com/cymmint/
* Ver: SFocus() 0.2
* Date: 2015-01-06
* Author: cymmint
* Function: SFocus是基本jQuery的banner(焦点图)切换插件,可以通过参数配置Box宽高,
* 点的大小、位置,是否显示Prev,Next,Dot以及Dot背景条,
* 首次显示等几张banner等
*/
(function($){ $.fn.SFocus = function(opts){ var defaults = {
oBoxWidth : "100%", //Silde width
oBoxHeight: 350, //Silde heigh
isOpenDBg : false, //open dot background bar
isOpenDot : true, //open do
isOpenPN : true, //open prev/next
dotWidth : 10, //dot width
dotHeight : 10, //dot heigh
dotLeft : "50%", //dot left offse
dotBottom : 16, //dot bottom offse
time : 3500, //cut time interval
onset : 0, //Silde focus img onset index
cutEvent : "mouseenter" //Slide dot cut even }; var opts = $.extend(defaults, opts); return this.each(function(){
var obox = $(this),
img = obox.find("ul.f_img li"),
sum = img.length,
cur_id = opts.onset >= sum ? 0 : opts.onset,
new_id = cur_id == sum-1 ? 0 : cur_id+1,
T = 0; //Html init
init(obox, sum, cur_id); //Dot cut
if(opts.isOpenDot) {
var dot = obox.find("div.f_dot a"); dot.on(opts.cutEvent, function(){
clearInterval(T); if ($(this).hasClass("on")) {
return false;
}
cur_id = img.parent().find(".on").index();
new_id = $(this).index(); cut(cur_id, new_id, img, dot);
});
} //Prev&Next cut
if(opts.isOpenPN) {
var prev = obox.find("a.f_prev"),
next = obox.find("a.f_next"); prev.on("click", function(){
clearInterval(T);
cur_id = img.parent().find(".on").index();
new_id = cur_id - 1;
new_id = new_id < 0 ? sum-1 : new_id; cut(cur_id, new_id, img, dot);
}); next.on("click", function(){
clearInterval(T);
cur_id = img.parent().find(".on").index();
new_id = cur_id + 1;
new_id = new_id >= sum ? 0 : new_id; cut(cur_id, new_id, img, dot);
});
} obox.hover(function(){
clearInterval(T);
if(opts.isOpenPN) {
prev.show();
next.show();
}
}, function(){
T = setInterval(function(){ auto(img, dot, sum);}, opts.time);
if(opts.isOpenPN) {
prev.hide();
next.hide();
}
}); //Slide run auto
T = setInterval(function(){ auto(img, dot, sum);}, opts.time);
}); //Slide run auto
function auto(img, dot, sum){
var cur_id = img.parent().find(".on").index(),
new_id = cur_id + 1;
new_id = new_id >= sum ? 0 : new_id; cut(cur_id, new_id, img, dot);
} //Slide cut
function cut(cur_id, new_id, img, dot){
if(opts.isOpenDot) {
dot.removeClass("on").eq(new_id).addClass("on");
} img.finish();
img.removeClass("on");
img.eq(cur_id).css("zIndex", 1); img.eq(new_id).addClass("on").css("zIndex",2);
img.eq(new_id).stop(true, false).fadeIn(500, function(){
img.eq(cur_id).hide();
});
} //Html init
function init(obox, sum, cur_id){
var htm = "",
dot,
img; if(opts.isOpenDBg) {
htm = '<div class="f_d_bg"></div>';
} if(opts.isOpenDot) {
htm += '<div class="f_dot">';
for(var i=0; i<sum; i++) {
htm += i == cur_id ? '<a class="on" href="javascript:;"></a>' : '<a href="javascript:;"></a>';
}
htm += '</div>';
} if(opts.isOpenPN) {
htm += '<a class="f_prev" href="javascript:;">&lt;</a><a class="f_next" href="javascript:;">&gt;</a>';
} obox.append(htm);
img = obox.find("ul.f_img");
dot = obox.find("div.f_dot"); if (!$.support.leadingWhitespace) {
obox.find("a").attr("hidefocus", true);
}
obox.css({"width":opts.oBoxWidth, "height":opts.oBoxHeight});
img.children().eq(cur_id).addClass("on").css({"display":"list-item", "zIndex":2});
dot.children().css({"width":opts.dotWidth, "height":opts.dotHeight});
dot.css({"width":dot.children().eq(0).outerWidth(true) * sum, "height":opts.dotHeight, "left":opts.dotLeft, "bottom":opts.dotBottom, "marginLeft": -(dot.width()-10)/2});
}
} })(jQuery);
</script> </body>
</html>

展开全部

jQuery banner切换插件的更多相关文章

  1. 推荐几款jquery图片切换插件

    一.前言 毕业季到了,大家都在匆匆忙忙的记录大学里最美好的时光,照片中各种花式.各种姿势都涌现出来了.这么多的照片怎么展示出来给自己的好友看呢?有人选择做成视频,有人选择ps之后做成图片集,而我选择利 ...

  2. FlexSlider jQuery滑动切换插件 参数

    demo:http://www.sucaihuo.com/jquery/0/6/demo/ FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所 ...

  3. FlexSlider是一个非常出色的jQuery滑动切换插件

    FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所有初级和高级网页设计师使用.不过很多人都只是使用默认的参数,今天来说说具体的参数来给大家看看 ...

  4. jQuery图片切换插件jquery.cycle.js

    Cycle是一个很棒的jQuery图片切换插件,提供了非常好的功能来帮助大家更简单的使用插件的幻灯功能 下载cycle插件并引入,此时,注意把引入它的代码放在引入jQuery主文件之后. <he ...

  5. jquery图片切换插件jquery.cycle.js参数详解

    转自:国人的力量 blog.163.com/xz551@126/blog/static/821257972012101541835491/ 自从使用了jquery.cycle.js,我觉得再也不用自己 ...

  6. jquery简单切换插件

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

  7. [转]8款实用的jQuery/CSS3最新插件应用

    今天给大家分享8款非常酷的最新jQuery/CSS3应用插件,废话少说,下面一起来看看这些插件吧. 1.HTML5重力感应积木游戏 这也是一款基于HTML5技术的重力感应游戏,一些积木从天而降,你可以 ...

  8. 基于jQuery的对象切换插件:soChange 1.5 (点击下载)

    http://www.jsfoot.com/jquery/demo/2011-09-20/192.html 所有参数: $(obj).soChange({     thumbObj:null, //导 ...

  9. jQuery动画切换引擎插件Velocity.js

    Velocity.js 官网 Velocity.js实现弹出式相框 慕课网 极棒的jquery动画切换引擎插件Velocity.js jQ库 (function($){ // 普通调用 /*$('#d ...

随机推荐

  1. 【HDU 3487】Play with Chain Splay

    题意 给定$n$个数序列,每次两个操作,将区间$[L,R]$拼接到去掉区间后的第$c$个数后,或者翻转$[L,R]$ Splay区间操作模板,对于区间提取操作,将$L-1$ Splay到根,再将$R+ ...

  2. codeforces 706A A. Beru-taxi(水题)

    题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...

  3. POJ3660(foyld闭包问题)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8794   Accepted: 4948 Descr ...

  4. cocos2dx unzip、createDir

    转自:http://www.cnblogs.com/xioapingguo/p/4037323.html static unsigned long _maxUnzipBufSize = 0x50000 ...

  5. python set集合的用法

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...

  6. 原生app与WebApp的区别

    Native App开发Native App开发即我们所称的传统APP开发模式(原生APP开发模式),该开发针对IOS.Android等不同的手机操作系统要采用不同的语言和框架进行开发,该模式通常是由 ...

  7. B - Simple Game

    B - Simple Game Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Su ...

  8. Ubuntu&nbsp;Navicat&nbsp;for&amp;nbs…

    首先上官网上下载LINUX版本: http://www.navicat.com/download 1. 下载 navicat110_mysql_en.tar.gz 文件 2. 下载后解压tar文件 t ...

  9. ADT-Bundle--Android开发环境快速搭建

    http://blog.csdn.net/aizquan/article/details/8974750

  10. dialog 设置maxHeight 最大高度

    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Displ ...