jQuery banner切换插件
今天学写了一个基于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:;"><</a><a class="f_next" href="javascript:;">></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:;"><</a><a class="f_next" href="javascript:;">></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切换插件的更多相关文章
- 推荐几款jquery图片切换插件
一.前言 毕业季到了,大家都在匆匆忙忙的记录大学里最美好的时光,照片中各种花式.各种姿势都涌现出来了.这么多的照片怎么展示出来给自己的好友看呢?有人选择做成视频,有人选择ps之后做成图片集,而我选择利 ...
- FlexSlider jQuery滑动切换插件 参数
demo:http://www.sucaihuo.com/jquery/0/6/demo/ FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所 ...
- FlexSlider是一个非常出色的jQuery滑动切换插件
FlexSlider是一个非常出色的jQuery滑动切换插件,它支持所有主流浏览器,并有淡入淡出效果.适合所有初级和高级网页设计师使用.不过很多人都只是使用默认的参数,今天来说说具体的参数来给大家看看 ...
- jQuery图片切换插件jquery.cycle.js
Cycle是一个很棒的jQuery图片切换插件,提供了非常好的功能来帮助大家更简单的使用插件的幻灯功能 下载cycle插件并引入,此时,注意把引入它的代码放在引入jQuery主文件之后. <he ...
- jquery图片切换插件jquery.cycle.js参数详解
转自:国人的力量 blog.163.com/xz551@126/blog/static/821257972012101541835491/ 自从使用了jquery.cycle.js,我觉得再也不用自己 ...
- jquery简单切换插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- [转]8款实用的jQuery/CSS3最新插件应用
今天给大家分享8款非常酷的最新jQuery/CSS3应用插件,废话少说,下面一起来看看这些插件吧. 1.HTML5重力感应积木游戏 这也是一款基于HTML5技术的重力感应游戏,一些积木从天而降,你可以 ...
- 基于jQuery的对象切换插件:soChange 1.5 (点击下载)
http://www.jsfoot.com/jquery/demo/2011-09-20/192.html 所有参数: $(obj).soChange({ thumbObj:null, //导 ...
- jQuery动画切换引擎插件Velocity.js
Velocity.js 官网 Velocity.js实现弹出式相框 慕课网 极棒的jquery动画切换引擎插件Velocity.js jQ库 (function($){ // 普通调用 /*$('#d ...
随机推荐
- java对象的初始化过程和创建对象的几种方式
1.加载父类,加载父类的静态属性和静态代码块 2.加载子类,加载子类的静态属性和静态代码块 3.初始化父类中的非静态属性并赋初值,执行父类非静态代码块,执行父类构造. 4.初始化子类中的非静态属性并赋 ...
- linux命令学习笔记(45):free 命令
free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的 工具中,free命令是最经常使用的命令之一. .命令格式: free [参 ...
- 浅析linux 下shell命令执行和守护进程
执行shell脚本有以下几种方式 1.相对路径方式,需先cd到脚本路径下 [root@banking tmp]# cd /tmp [root@banking tmp]# ./ceshi.sh 脚本执行 ...
- PS 滤镜— —球面化效果
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- POJ2657Comfort(扩展欧几里得基础)
A game-board consists of N fields placed around a circle. Fields are successively numbered from1 to ...
- 「NOIP2006」「LuoguP1064」 金明的预算方案(分组背包
题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过NNN元钱就行” ...
- 【Lintcode】136.Palindrome Partitioning
题目: Given a string s, partition s such that every substring of the partition is a palindrome. Return ...
- springboot集成报错,想要集成tk.mybatis报错,反射方法异常
在添加注释 @MapperScan("com.leyou.item.mapper")的时候,如果不小心就会导包倒错应该导成 import tk.mybatis.spring.ann ...
- Repeater 和 GridView 中数据格式化
GridView中显示两位小数: <asp:BoundField DataField="investmoney" DataFormatString="{0:f2}& ...
- 使用API修改控制台输出的颜色(前景色和背景色)
1.api原型 SetConsoleTextAttribute BOOL WINAPI SetConsoleTextAttribute( _In_ HANDLE hConsoleOutput, // ...