注:来源于网络,整理而成
html文件:index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HappyImage</title>
<style>
@import url("HapplyImage.css");
</style>
</head>
<body>
<p>滑动效果</p>
<div class="target" id="target-1">
<div>
<div><img src="t1.png"></div>
<div><img src="t2.png"></div>
<div><img src="t3.png"></div>
<div><img src="t4.png"></div>
</div>
</div>
<br>
<br>
<p>淡入效果</p>
<div class="target" id="target-2">
<div>
<div><img src="t4.png"></div>
<div><img src="t3.png"></div>
<div><img src="t2.png"></div>
<div><img src="t1.png"></div>
</div>
</div>
<script src="jquery.min.js"></script>
<script src="HappyImage.min.js"></script>
<script>
$("#target-1").HappyImage({
effect: "slide"
});
$("#target-2").HappyImage({
effect: "fade"
});
</script>
</body>
</html>
展示:
CSS文件:HapplyImage.css
body {
background-color: #1F1F1F
}
p {
text-align: center;
font-size: 30px;
margin: 20px 0;
color: aliceblue;
}
.target {
width: 700px;
height: 300px;
overflow: hidden;
position: relative;
margin: 20px auto;
}
.target img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
js文件:HapplyImage.js
/*! HappyImage v1.0.0 | (c) 2019 ZG | MIT license */ !(function() {
'use strict';
function _typeof(c) {
_typeof = "function" === typeof Symbol && "symbol" === typeof Symbol.iterator ? function(a) {
return typeof a
} : function(a) {
return a && "function" === typeof Symbol && a.constructor === Symbol && a !== Symbol.prototype ? "symbol" : typeof a
};
return _typeof(c)
}! function(c, a) {
"function" === typeof define && define.amd ? define(["jquery"], function(g) {
return a(c, g)
}) : "undefined" !== typeof module && "object" === ("undefined" === typeof exports ? "undefined" : _typeof(exports)) ? a(c, require("jquery")) : a(c, c.jQuery)
}("undefined" !== typeof window ? window : void 0, function(c, a) {
! function() {
if("undefined" === typeof jQuery) throw Error("HappyImage's JavaScript requires jQuery");
var l = a.fn.jquery.split(".");
if(1 === ~~l[0] && 8 > ~~l[1]) throw Error("HappyImage's JavaScript requires jQuery version 1.8.0 or higher");
}();
var g = navigator.userAgent.toLowerCase(),
u = [g.match("msie 9.0"), g.match("msie 10.0")],
x = u[0],
C = u[1];
g.match(/msie (8|7|6)/) || (a("style").filter('[data-from\x3d"HappyImage"]').remove(), a("\x3cstyle\x3e").html(".hy-target{overflow:hidden}.hy-relative{position:relative}.hy-wrapper{overflow:hidden;position:absolute;top:0;left:0;height:100%}.hy-wrapper *{box-sizing:border-box}.hy-fade .hy-box{position:absolute;width:100%;height:100%;top:0;left:0;opacity:0;-webkit-transition-property:opacity;-moz-transition-property:opacity;transition-property:opacity;-webkit-transition-timing-function:ease-in-out;-moz-transition-timing-function:ease-in-out;transition-timing-function:ease-in-out}.hy-box.hy-fade-show{opacity:1}.hy-slide{display:-webkit-flex;display:-moz-flex;display:flex;justify-content:space-between;-webkit-transition-timing-function:cubic-bezier(.6,0,.52,1);-moz-transition-timing-function:cubic-bezier(.6,0,.52,1);transition-timing-function:cubic-bezier(.6,0,.52,1)}.hy-slide .hy-box{position:relative}.hy-box{overflow:hidden}.hy-next,.hy-prev{position:absolute;top:50%;-webkit-transform:translate(0,-50%);-moz-transform:translate(0,-50%);-ms-transform:translate(0,-50%);transform:translate(0,-50%);z-index:9;width:40px;height:40px;border-radius:50%;overflow:hidden;cursor:pointer;background:rgba(0,0,0,.3);-webkit-transition:opacity .35s;-moz-transition:opacity .35s;transition:opacity .35s}.hy-next i,.hy-prev i{position:absolute;top:50%;left:50%;-webkit-transform:translate(-50%,-50%);-moz-transform:translate(-50%,-50%);-ms-transform:translate(-50%,-50%);transform:translate(-50%,-50%);display:block;width:14px;height:14px;opacity:.7;-webkit-transition:.2s;;-moz-transition:.2s;transition:.2s;background:url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBzdGFuZGFsb25lPSJubyI/PjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjEvRFREL3N2ZzExLmR0ZCI+PH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TM3Mi0zODYuMTExMDc5MTY0MTM3MmMxMi4yNTc0OTUwNTU0NDc2NTItMTIuMjU3NDk1MDU1NDQ3NjUyIDEyLjI1NzQ5NTA1NTQ0NzY1Mi0zMC42NDM3MzU5NjI3ODk4IDAtNDIuOTAxMjMxMDE4MjM3NDdMMzQ2LjUyMzgyMzQ1NDc3NDA2IDEwNC40MzgzMDUzMjY3NDQxN3oiIGZpbGw9IiNmZmZmZmYiIHAtaWQ9IjExMDMiPjwvcGF0aD48L3N2Zz4\x3d) center no-repeat}.hy-next:hover i,.hy-prev:hover i{opacity:1}.hy-prev{left:20px;-webkit-transform:translate(0,-50%) rotate(180deg);-moz-transform:translate(0,-50%) rotate(180deg);-ms-transform:translate(0,-50%) rotate(180deg);transform:translate(0,-50%) rotate(180deg)}.hy-next{right:20px}.hy-next.hy-hovershow,.hy-prev.hy-hovershow{opacity:0}.hy-target:hover .hy-next.hy-hovershow,.hy-target:hover .hy-prev.hy-hovershow{opacity:1}.hy-dot{position:absolute;bottom:15px;z-index:9;height:12px}.hy-dot i{display:block;float:left;width:10px;height:10px;border-radius:50%;cursor:pointer;margin:0 5px;background:rgba(0,0,0,.3);-webkit-transition:.4s;-moz-transition:.4s;transition:.4s}.hy-dot i.active{background:#fff}.hy-dot-left{left:10px}.hy-dot-center{left:50%;-webkit-transform:translate(-50%,0);-moz-transform:translate(-50%,0);-ms-transform:translate(-50%,0);transform:translate(-50%,0)}.hy-dot-right{right:10px}").attr("data-from", "HappyImage").prependTo("head"), a.fn.HappyImage = function() {
var l = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : {};
return this.each(function() {
function k(a) {
if(!n) {
n = !0;
if("fade" === b.effect) {
d = a;
if(x) p.eq(a).animate({
opacity: 1
}, t).siblings().animate({
opacity: 0
}, t, function() {
n = !1
});
else p.eq(a).addClass("hy-fade-show").siblings().removeClass("hy-fade-show").on("transitionend", function() {
n = !1
});
z(a);
u && b.onChange(a)
}
if("slide" === b.effect) {
if(x) {
var l = f.find(".hy-ie9-mark");
l.animate({
textIndent: "".concat(-100 / e * (a + 1), "%")
}, {
step: function(a) {
h.css("transform", "translate(".concat(a, "%, 0)"))
},
duration: t,
complete: function() {
n = !1;
d = a;
if(d === e - 2) {
var b = "-".concat(100 / e, "%");
h.css("transform", "translate(".concat(b, ", 0)"));
l.css("textIndent", b);
d = 0
} - 1 === d && (b = "-".concat(100 / e * (e - 2), "%"), h.css("transform", "translate(".concat(b, ", 0)")), l.css("textIndent", b), d = e - 3)
}
})
} else h.css({
transitionDuration: "".concat(t, "ms"),
transform: "translate(-".concat(100 / e * (a + 1), "%, 0)")
}).on("transitionend", function() {
n = !1;
d = a;
h.off("transitionend").css("transitionDuration", "0s");
d === e - 2 && (h.css("transform", "translate(-".concat(100 / e, "%, 0)")), d = 0); - 1 === d && (h.css("transform", "translate(-".concat(100 / e * (e - 2), "%, 0)")), d = e - 3)
});
z(a === e - 2 ? 0 : a);
if(u) {
var c = a; - 1 === c && (c = e - 3);
c === e - 2 && (c = 0);
b.onChange(c)
}
}
}
}
function z(a) {
v.eq(a).addClass("active").siblings().removeClass("active")
}
function A() {
d++;
"fade" === b.effect && d > e - 1 && (d = 0);
k(d)
}
var q = {
width: null,
height: null,
effect: "slide",
duration: 700,
arrow: !0,
dot: !0,
defaultIndex: 0,
arrowHoverShow: !1,
dotAlign: "center",
autoplay: 0,
onChange: a.noop
},
b = a.extend({}, q, l),
f = a(this),
h = f.children(),
p = h.children(),
e = p.length,
g = Math.random().toString(36).substr(2);
f.data({
happyImageOptions: b,
happyImageContent: f.clone(!0),
happyImageID: g
});
var u = a.isFunction(b.onChange),
t = a.isNumeric(b.duration) && 0 < b.duration ? b.duration : q.duration,
d = a.isNumeric(b.defaultIndex) && 0 < b.defaultIndex && b.defaultIndex <= e - 1 ? parseInt(b.defaultIndex) : 0;
if(1 === h.length && e) {
f.addClass("hy-target");
h.addClass("hy-wrapper");
p.addClass("hy-box");
a.isNumeric(b.width) && 0 < b.width && f.width(b.width);
a.isNumeric(b.height) && 0 < b.height && f.height(b.height);
"static" === f.css("position") && f.addClass("hy-relative");
"slide" === b.effect && (e += 2, h.prepend(p.last().clone(!0)).append(p.first().clone(!0)).width("".concat(100 * e, "%")).addClass("hy-slide").css("transform", "translate(-".concat(100 * (d + 1) / e, "%, 0)")).children().width("".concat(100 / e, "%")), (x || C) && h.removeClass("hy-slide").children().css({
float: "left",
position: "relative"
}).height(h.height()), x && (f.find(".hy-ie9-mark").remove(), a("\x3cdiv\x3e").css({
textIndent: "".concat(100 * -(d + 1) / e, "%"),
display: "none"
}).addClass("hy-ie9-mark").appendTo(f)));
if("fade" === b.effect) {
h.width("100%").addClass("hy-fade");
p.eq(d).addClass("hy-fade-show");
var D = c.setTimeout(function() {
p.css("transitionDuration", "".concat(t, "ms"));
c.clearTimeout(D)
}, 0)
}
var r = [a(""), a("")],
q = r[0],
r = r[1];
!1 !== b.arrow && (!0 === b.arrow && (f.append('\n\t\t\t\t\t\t\x3cdiv class\x3d"hy-prev"\x3e\x3ci\x3e\x3c/i\x3e\x3c/div\x3e\n\t\t\t\t\t\t\x3cdiv class\x3d"hy-next"\x3e\x3ci\x3e\x3c/i\x3e\x3c/div\x3e\n\t\t\t\t\t'), q = f.find(".hy-prev"), r = f.find(".hy-next"), b.arrowHoverShow && q.add(r).addClass("hy-hovershow")), Array.isArray(b.arrow) && 2 === b.arrow.length && (q = a(b.arrow[0]), r = a(b.arrow[1]), q.add(r).addClass("hy-custom-arrow").data("happyImageID", g)));
var w = [a(""), a("")],
m = w[0],
v = w[1];
if("boolean" === a.type(b.dot)) {
w = "slide" === b.effect ? e - 2 : e;
m = "";
if("function" === typeof String.prototype.repeat) m = "\x3ci\x3e\x3c/i\x3e".repeat(w);
else
for(var B = 0; B < w; B++) m += "\x3ci\x3e\x3c/i\x3e";
f.append('\x3cdiv class\x3d"hy-dot"\x3e'.concat(m, "\x3c/div\x3e"));
m = f.find(".hy-dot");
v = m.children();
m.addClass("hy-dot-".concat(b.dotAlign));
v.eq(d).addClass("active");
!1 === b.dot && m.hide()
}
"string" === a.type(b.dot) && (m = a(b.dot).first(), v = m.children(), v.addClass("hy-custom-dot").data("happyImageID", g));
var n = !1;
r.on("click.hy", function() {
n || (d++, "fade" === b.effect && d > e - 1 && (d = 0), k(d))
});
q.on("click.hy", function() {
n || (d--, "fade" === b.effect && 0 > d && (d = e - 1), k(d))
});
m.on("click.hy", "i", function() {
n || a(this).hasClass("active") || (d = a(this).index(), k(d))
});
var y = null;
a.isNumeric(b.autoplay) && b.autoplay > t && (y = c.setInterval(A, ~~b.autoplay), f.on({
mouseenter: function() {
return c.clearInterval(y)
},
mouseleave: function() {
y = c.setInterval(A, ~~b.autoplay)
}
}))
}
})
}, a.fn.destroyHappyImage = function() {
return this.each(function() {
var c = a(this),
k = c.data("happyImageOptions");
if(a.isPlainObject(k) && !a.isEmptyObject(k)) {
var g = c.data("happyImageID");
c.replaceWith(c.data("happyImageContent"));
a(".hy-custom-arrow, .hy-custom-dot").each(function() {
var c = a(this);
c.data("happyImageID") === g && (c.removeData("happyImageID").removeClass("hy-custom-arrow hy-custom-dot").off("click.hy"), c.filter(".hy-custom-dot").parent().off("click.hy"))
})
}
})
}, c.HappyImage = function(c, k) {
c = a(c);
var g = c.find(".hy-dot i"),
l = c.data("happyImageOptions");
a.isPlainObject(l) && !a.isEmptyObject(l) && a.isNumeric(k) && 0 <= k && k < g.length && g.filter(".active").index() !== k && c.find(".hy-dot i").eq(k).trigger("click.hy")
})
});
})();