这个效果主要有两个特点:

1. 背景切换的渐变

2. 背景大小自适应

3. 背景自适应保持比例同时, 相对居中

js源码:

(function ($) {
$.fn.bgChange = function (options) {
var defaults = {
width: 1920,
height: 1080,
changeRate: 3000,
isLoadLimit: false,
loadLimit: 10000,
changed: function () { },
top: 0,
loaded: function () { }
};
var opts = $.extend(defaults, options); var event = {
loaded: opts.loaded,
picsLoaded: picsLoaded,
changed: opts.changed
}; var picHolder = $(this);
var pics = $(picHolder).find('.bgpic img'); $(pics).attr('status', 'ready'); var vl = {
index: 0,
wWidth: 0,
wHeigth: 0,
rate: opts.width / opts.height,
total: pics.length,
predex: -1,
loadCount: 0,
timer: null,
lock: false,
isLoaded: false
}; var obj = {
start: start,
pause: pause,
next: next,
pre: pre,
total: vl.total
}; function start() {
if (vl.isLoaded) {
if (vl.timer == null) {
vl.timer = setInterval(changePic, opts.changeRate);
}
}
} function pause() {
if (vl.isLoaded) {
if (vl.timer != null) {
clearInterval(vl.timer);
vl.timer = null;
}
}
} function next() {
if (vl.isLoaded) {
pause();
if (!vl.lock) {
var indexT = vl.index;
if (vl.predex >= 0 && vl.predex < vl.total) {
$(pics).eq(vl.predex).css('z-index', 1).stop(false, false).fadeOut(500, function () {
$(this).css('z-index', 0);
vl.lock = false;
});
$(pics).eq(indexT).stop(false, false).show();
}
event.changed({ index: indexT, total: vl.total });
vl.predex = vl.index;
vl.index = vl.index < (vl.total - 1) ? vl.index + 1 : 0; start();
}
vl.lock = true;
}
} function pre() {
if (vl.isLoaded) {
pause();
if (!vl.lock) {
var indexT = (vl.index - 2) >= 0 ? (vl.index - 2) : (vl.total + vl.index - 2);
if (vl.predex >= 0 && vl.predex < vl.total) {
$(pics).eq(vl.predex).css('z-index', 1).stop(false, false).fadeOut(500, function () {
$(this).css('z-index', 0);
vl.lock = false;
});
$(pics).eq(indexT).stop(false, false).show();
}
event.changed({ index: indexT, total: vl.total });
vl.predex = indexT;
vl.index = indexT < (vl.total - 1) ? indexT + 1 : 0; start();
}
vl.lock = true;
}
} function setSize() {
vl.wWidth = $(window).width();
vl.wHeight = $(window).height() - opts.top;
var nWidth = 0;
var nHeight = 0;
if (vl.rate > vl.wWidth / vl.wHeight) {
nWidth = vl.wHeight * vl.rate;
nHeight = vl.wHeight;
setPicSize(pics, 'auto', vl.wHeight);
} else {
nWidth = vl.wWidth;
nHeight = vl.wWidth / vl.rate;
setPicSize(pics, vl.wWidth, 'auto');
}
//$(picHolder).css({ 'height': vl.wHeight, 'left': -(nWidth - vl.wWidth) / 2 });
$(picHolder).css({ 'width': vl.wWidth, 'height': vl.wHeight }).find('.bgpic').css({ 'left': -(nWidth - vl.wWidth) / 2, 'top': -(nHeight - vl.wHeight) / 2 });
} function setPicSize(o, w, h) {
$(o).css({ 'width': w, 'height': h });
} $(window).resize(function () {
setSize();
}); setSize(); function changePic() {
var indexT = vl.index;
if (vl.predex >= 0 && vl.predex < vl.total) {
$(pics).eq(vl.predex).css('z-index', 1).stop(false, false).fadeOut(500, function () {
$(this).css('z-index', 0);
}); $(pics).eq(indexT).stop(false, false).show();
} else {
$(pics).eq(indexT).stop(false, false).fadeIn(500);
}
event.changed({ index: indexT, total: vl.total });
vl.predex = vl.index;
vl.index = vl.index < (vl.total - 1) ? vl.index + 1 : 0;
} $(pics).eq(0).show(); $(pics).each(function (i, val) {
$(val).load(function () {
$(val).attr('status', 'loaded');
vl.loadCount++;
if (vl.loadCount == vl.total) {
vl.isLoaded = true;
event.picsLoaded();
}
});
$(val).attr('src', $(val).attr('original'));
}); if (vl.isLoadLimit) {
setTimeout(function () {
if (!vl.isLoaded) {
$(pics).filter("[status='ready']").remove();
pics = $(picHolder).find('.bgpic img');
vl.total = pics.length;
obj.total = vl.total;
event.picsLoaded();
}
}, opts.loadLimit);
} function picsLoaded() {
start();
changePic();
event.loaded();
} return obj;
}
})(jQuery);

css源码:

body
{
padding:;
margin:;
}
#bgpics
{
position: absolute;
width: 100%;
z-index:;
left:;
top:;
overflow: hidden;
}
.bgpic
{
position: relative;
}
#bgpics .bgpic img
{
position: absolute;
display: none;
z-index:;
}

使用:

<!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>
<title></title>
<script src="jquery-1.8.2.min.js" type="text/javascript"></script>
<link href="bgChange.css" rel="stylesheet" type="text/css" />
<script src="bgChange.js" type="text/javascript"></script>
<script>
$(function () {
$('#bgpics').bgChange({changeRate: 1000,loaded: function () { alert('loaded!') }});
});
</script>
</head>
<body>
<div id="bgpics">
<div class="bgpic">
<img original="slide/001.jpg" />
<img original="slide/002.jpg" />
<img original="slide/003.jpg" />
<img original="slide/004.jpg" />
<img original="slide/005.jpg" />
<img original="slide/006.jpg" />
</div>
</div>
</body>
</html>

使用很简单, 同时bgChange()可以传入一些参数, 达到效果的调整, 例如切换速度.

暂时不进行解析和注释, 可能找个时间再补上, 前段时间写的, 逻辑有点遗忘

效果请大家自行下载, 打开htm文件即可.

下载地址: http://yunpan.cn/Q757aP3PS2cqa (提取码:fed4)

分享一个自己写的基于JQuery的一个Web背景切换的Demo的更多相关文章

  1. 基于jQuery扁平多颜色选项卡切换代码

    基于jQuery扁平多颜色选项卡切换代码,支持自动轮播切换,鼠标滑过切换的jQuery特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class=" ...

  2. 基于jQuery图片遮罩滑动文字切换特效

    基于jQuery图片遮罩滑动文字切换特效.这是一款jquery hover鼠标滑动选项卡切换透明背景遮罩文字显示特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div ...

  3. 基于jQuery左侧大图右侧小图切换代码

    基于jQuery左侧大图右侧小图切换代码是一款带右侧缩略图选项卡的jQuery图片切换特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class=" ...

  4. 一款基于jquery超炫的图片切换特效

    今天为给大家介绍一款基于jquery超炫的图片切换特效.由百叶窗飞入显示图片.图片消息的时候也是百叶窗渐行渐远.用于图片展示,效果还是非常好,我们一起看下效果图: 在线预览   源码下载 来看下实现的 ...

  5. 基于jQuery的自适应图片左右切换

    效果预览:http://hovertree.com/code/jquery/smjv6d0n.htm 基于jQuery编写的横向自适应幻灯片切换特效 全屏自适应jquery焦点图切换特效,在IE6这个 ...

  6. 基于jQuery环形图标菜单旋转切换特效

    分享一款基于jQuery环形图标旋转切换特效.这是一款鼠标点击图标菜单圆形顺时针或者逆时针旋转切换代码.效果图如下: 在线预览   源码下载 实现的代码. js代码: /* 图片地址可以是相对路径或绝 ...

  7. 分享一个以前写的基于C#语言操作数据库的小框架

    一:前言 这个是以前写的操作MySQL数据库的小型框架,如果是中小型项目用起来也是很不错的,里面提供Filter.ModelPart.Relationship等机制实现操作数据库时的SQL语句的拼接和 ...

  8. 分享一个自己写的基于canvas的原生js图片爆炸插件

    DEMO访问地址: https://bupt-hjm.github.io/BoomGo/博客地址: http://bupt-hjm.github.io/2016/07/10/boom/插件及使用方法地 ...

  9. 分享一个自己写的基于TP的关系模型

    为了说明问题,假设现在有表test1,test1有从表test2:test1属于test3,test1和test4多对多,关联表test1_test4. 1.定义关系 class Test1Model ...

随机推荐

  1. Python网络爬虫(6)--爬取淘宝模特图片

    经过前面的一些基础学习,我们大致知道了如何爬取并解析一个网页中的信息,这里我们来做一个更有意思的事情,爬取MM图片并保存.网址为https://mm.taobao.com/json/request_t ...

  2. SVG Loading

    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="64&qu ...

  3. 黑马程序员—— Java SE(2)

    ----<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训 ...

  4. 1228.1——计算器(未使用MVC设计模式)

    #import "ViewController.h"typedef enum{    kStausNum,    kStausOperation}kStaus; typedef e ...

  5. UISwitch 监听响应

    UISwitch *swh = [[UISwitch alloc]initWithFrame:CGRectMake(100,100, 50, 30)];        swh.on = YES;    ...

  6. I - u Calculate e

    Description A simple mathematical formula for e is where n is allowed to go to infinity. This can ac ...

  7. popup

    http://vast-engineering.github.com/jquery-popup-overlay/ http://photoswipe.com/

  8. mysql之主从复制

    原理--> 在数据库层面,复制语句或者行,因为在数据库层面,故只有主服务器生成,并放到二进制日志里面,才能复制给从服务器. 原理--> mysql的主从复制基于异步,主要有三个进程执行,分 ...

  9. Super Jumping! Jumping! Jumping!(hdu 1087 LIS变形)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  10. ZOJ 3745 Salary Increasing

    Description Edward has established a company with n staffs. He is such a kind man that he did Q time ...