Perfectly compatible with the major browsers of jQuery plug in to achieve image switching effects
- 2020-03-30 04:35:19
- OfStack
The functional notes in the file are also written in great detail (see zoedylan.imgchange-1.0.1.js file for details), which is very helpful for netizens to learn, although the style is not very good, you can write, make good use of oh...
JS code part:
(function ($) {
var//Declare the global variable
_eleTemp,//Cache variable
_eleThis = $(this),//Current element
_eleImg = $('.zd-imgChange-img'),//Image group element
_eleControll = $('.zd-imgChange-controll'),//Controller group element
_eleChange = $('.zd-imgChange-change'),//Toggle
_icon = ' "" ',//Dynamic icon
_imgTemplate = $(document.createElement('img')),//Image template '
_setting = {
height: 100,//< br / >
width: 200,//< br / >
imgs: new Array(),//Image address
links: null,//Click the address
tips: null,//
timers: 3000//Automatic switching time
},//Configuration < br / >
_timers = null//Automatically toggles the save variable
$.fn.zoeDylan_ImageChange = function (op) {
_eleThis = $(this);
_setting = $.extend(_setting, op);//Set the property
//Processing data (to see if it is a valid range)
_setting.timers = _setting.timers != null && _setting.timers != undefined && _setting.timers > 1000 ? _setting.timers : 1000;
_setting.height = parseFloat(_setting.height) < 100 ? 100 : parseFloat(_setting.height);
_setting.width = parseFloat(_setting.width) < 200 ? 200 : parseFloat(_setting.width);
return this.each(function () {//Initialize
AddTemplate();
AddImg();
DisSize();
DisControll();
})
}
//Add template
function AddTemplate() {
_eleThis.removeClass();
_eleThis.addClass('zd-imgChange');
_eleTemp = '<button class="zd-imgChange-change zd-imgChange-change-left"><</button>';
_eleThis.append(_eleTemp);
_eleTemp = ' <div class="zd-imgChange-img"> </div>';
_eleThis.append(_eleTemp);
_eleTemp = ' <div class="zd-imgChange-controll"></div>';
_eleThis.append(_eleTemp);
_eleTemp = ' <button class="zd-imgChange-change zd-imgChange-change-right">></button>';
_eleThis.append(_eleTemp);
//Assign a value to the global variable
_eleImg = $('.zd-imgChange-img');
_eleControll = $('.zd-imgChange-controll');
_eleChange = $('.zd-imgChange-change');
}
//Add image
function AddImg() {
for (var i = 0; i < _setting.imgs.length; i++) {
//Picture < br / >
_eleTemp = $(document.createElement('img'));
_eleTemp.addClass('zd-imgChange-img-item');
_eleTemp.attr('src', _setting.imgs[i]);
_eleImg.append(_eleTemp);
//Icon < br / >
_eleControll.append('<span>' + _icon[1] + '</span>');
//Tip < br / >
$(_eleImg.children('img')[i]).attr({ 'title': _setting.tips[i] + '|' + _setting.links[i] });
$(_eleControll.children('span')[i]).attr('title', _setting.tips[i]);
}
Dispose(0, 'TtoB');
}
//Processing size
function DisSize() {
_eleThis.css({ 'height': _setting.height, 'width': _setting.width, 'font-size': _setting.height * 0.2 - 2 });
_eleThis.children('button').css('font-size', _setting.height * 0.2 - 2);
_eleChange.css({ 'line-height': _setting.height + 'px' });
}
//Handle the operation event
function DisControll() {
_eleImg.children('img').bind('click', function () {//Click on the image to jump to
document.location = 'http://' + $(this).attr('title').split('|')[$(this).attr('title').split('|').length - 1];
});
_eleChange.bind('click', function () {// Sequence switch Picture < br / >
var nowImg = $('.zd-imgChange-img-item-sel');
_eleTemp = _eleImg.children('img');
if ($(this).hasClass('zd-imgChange-change-left')) {//Toggle
to the left
for (var i = 0; i < _eleTemp.length; i++) {
if (nowImg.attr('title') == $(_eleTemp[i]).attr('title')) {
if ((i - 1) < 0) {
Dispose(_eleTemp.length - 1, 'RtoL');
} else {
Dispose(i - 1, 'RtoL');
}
break;
}
}
} else if ($(this).hasClass('zd-imgChange-change-right')) {//Toggle
to the right
for (var i = 0; i < _eleTemp.length; i++) {
if (nowImg.attr('title') == $(_eleTemp[i]).attr('title')) {
if ((i + 1) > _eleTemp.length - 1) {
Dispose(0, 'LtoR');
} else {
Dispose(i + 1, 'LtoR');
}
break;
}
}
} else {
return false;
}
});
_eleControll.children('span').bind('click', function () {
for (var i = 0; i < _eleControll.children('span').length; i++) {
if ($(_eleControll.children('span')[i]).attr('title') == $(this).attr('title')) {
if ($(_eleImg.children('img')[i]).attr('src') != $('.zd-imgChange-img-item-sel').attr('src')) {//Determines whether to click on the same TAB
Dispose(i, 'TtoB');
}
break;
}
}
});
}
//Switcher
function Dispose(eNum, dir) {//Switch pictures & NBSP; < br / >
clearTimeout(_timers);
DisposeAnm();
$(_eleControll.children('span')).html(_icon[1]);
$(_eleControll.children('span')[eNum]).html(_icon[0]);
$('.zd-imgChange-change-left').attr('title', eNum - 1 >= 0 ? $(_eleControll.children('span')[eNum - 1]).attr('title') : $(_eleControll.children('span')[_eleControll.children('span').length - 1]).attr('title'));
$('.zd-imgChange-change-right').attr('title', eNum + 1 <= _eleControll.children('span').length - 1 ? $(_eleControll.children('span')[eNum + 1]).attr('title') : $(_eleControll.children('span')[0]).attr('title'));
_timers = setTimeout(Timers, _setting.timers);
//Toggle animation - visual effect based on z-index
function DisposeAnm() {
$('.zd-imgChange-img-item').removeClass('zd-imgChange-img-item-temp');
$('.zd-imgChange-img-item-sel').removeClass('zd-imgChange-img-item-sel').addClass('zd-imgChange-img-item-temp');
_eleTemp = $(_eleImg.children('img')[eNum]).addClass('zd-imgChange-img-item-sel');
if (dir == 'TtoB') {
_eleTemp.css('top', -_setting.height);
_eleTemp.animate({ top: 0 }, 300);
} else if (dir == 'RtoL') {
_eleTemp.css('left', _setting.width);
_eleTemp.animate({ left: 0 }, 300);
} else if (dir == 'LtoR') {
_eleTemp.css('left', -_setting.width);
_eleTemp.animate({ left: 0 }, 300);
}
}
}
//Automatic switch clock
function Timers() {
_timers = setTimeout(Timers, _setting.timers);
var nowImg = $('.zd-imgChange-img-item-sel');
_eleTemp = _eleImg.children('img');
for (var i = 0; i < _eleTemp.length; i++) {
if (nowImg.attr('title') == $(_eleTemp[i]).attr('title')) {
if ((i - 1) < 0) {
Dispose(_eleTemp.length - 1, 'TtoB');
} else {
Dispose(i - 1, 'TtoB');
}
break;
}
}
}
})($)
CSS code section:
.zd-imgChange {
position: relative;
margin: auto;
padding: 0px;
min-width: 200px;
min-height: 100px;
background: rgba(0,0,0,0.5);
color: #fff;
cursor: pointer;
overflow: hidden;
}
.zd-imgChange-change {
cursor:pointer;
color:#fff;
margin: 0px;
padding: 0px;
position: relative;
background: rgba(0,0,0,0.5);
width: 10%;
height: 100%;
text-align: center;
opacity: 0.1;
z-index: 1;
-moz-transition: opacity 0.4s;
-o-transition: opacity 0.4s;
-webkit-transition: opacity 0.4s;
transition: opacity 0.4s;
border:none;
}
.zd-imgChange-change:hover {
opacity: 1;
}
.zd-imgChange-change-left {
float: left;
}
.zd-imgChange-change-right {
float: right;
}
.zd-imgChange-img {
z-index: 0;
padding: 0px;
position: absolute;
top: 0px;
left: 0px;
background: rgba(100,0,0,0.1);
width: 100%;
height: 100%;
}
.zd-imgChange-img-item {
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
border: none;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
-moz-transition: opacity 0.5s;
-o-transition: opacity 0.5s;
-webkit-transition: opacity 0.5s;
transition: opacity 0.5s;
opacity: 1;
z-index: -1;
}
.zd-imgChange-img-item-temp{
z-index:0;
}
.zd-imgChange-img-item-sel {
z-index: 1;
}
.zd-imgChange-controll {
z-index: 2;
padding: 0px;
position: absolute;
bottom: 0px;
width: 100%;
height: 20%;
background: rgba(0,0,0,0);
text-align: center;
-moz-transition: background 0.4s;
-o-transition: background 0.4s;
-webkit-transition: background 0.4s;
transition: background 0.4s;
text-shadow: 0px 0px 5px #000;
opacity: 0.7;
}
.zd-imgChange-controll:hover {
background: rgba(0,0,0,0.5);
opacity: 1;
}
.zd-imgChange-controll span {
-moz-transition: color 0.4s;
-o-transition: color 0.4s;
-webkit-transition: color 0.4s;
transition: color 0.4s;
}
.zd-imgChange-controll span:hover {
color: rgba(0,0,0,0.6);
}
HTML code section:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width">
<title> Image switching </title>
<script src="jquery-1.10.2.min.js"></script>
<link href="zoeDylan.ImgChange.css" rel="stylesheet" />
<script src="zoeDylan.ImgChange.js"></script>
<script>
var a_imgs = new Array(//Insert image address
'./1 (1).jpg',
'./1 (2).jpg',
'./1 (3).jpg',
'./1 (4).jpg'
),
a_links = new Array(//Click the image to jump to the url
'www.baidu.com',
'www.qq.com',
'www.google.com',
'www.zol.com'
),
a_tips = new Array(//Mouse docking prompt
' baidu ',
' tencent ',
' Google ',
' zhongguancun '
);
$(function () {
$('#imgc').zoeDylan_ImageChange({//Set the < br / >
width: 500,
height: 300,
imgs: a_imgs,
links: a_links,
tips: a_tips,
timers:2000
});
});
</script>
</head>
<body>
<div id="imgc">
<!-- <span class="zd-imgChange-change zd-imgChange-change-left"><</span>
<div class="zd-imgChange-img">
<img class="zd-imgChange-img-item" src="http://subject.9ria.com/html5game/images/t1f.jpg" alt=" Image loading error !" title=" prompt " />
</div>
<div class="zd-imgChange-controll"><span>d</></div>
<span class="zd-imgChange-change zd-imgChange-change-right">></span>-->
</div>
</body>
</html>
Ps: try to use a higher version of the browser, visual test browser: IE11 IE10 IE6 Google cheetah openg can run perfectly, IE6 transparent effect has a loss
The code download: (link: http://xiazai.jb51.net/201412/yuanma/zeodylanimgchange (jb51.net). Rar)