[
{
"url": "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1133216305,48749125&fm=26&gp=0.jpg",
"link": "https://www.jd.com",
"title": "京东",
"alt": "京东"
},
{
"url": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1552566827&di=e764ec5f93592fbfee214fa17f6b4ae5&imgtype=jpg&er=1&src=http%3A%2F%2Fpic.90sjimg.com%2Fdesign%2F00%2F07%2F77%2F64%2F561221d452fe4.jpg",
"link": "https://www.taobao.com",
"title": "淘宝",
"alt": "天猫"
},
{
"url": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1551972170779&di=3d03a9d4aa48b12fdf937b3dd06c8fa2&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01aa6d55d137156ac725baa0e6971c.jpg%401280w_1l_2o_100sh.png",
"link": "https://www.tmall.com",
"title": "天猫",
"alt": "天猫"
},
{
"url": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1552567034&di=50eac16f578786c7fe66ccc1e38a9acb&imgtype=jpg&er=1&src=http%3A%2F%2Fgss0.baidu.com%2F9fo3dSag_xI4khGko9WTAnF6hhy%2Fzhidao%2Fpic%2Fitem%2F1ad5ad6eddc451daebe2553fb4fd5266d11632c9.jpg",
"link": "https://www.baidu.com",
"title": "百度",
"alt": "百度"
},
{
"url": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1551823210051&di=8c1c79ff108688568f9a9af8954a0bd0&imgtype=jpg&src=http%3A%2F%2Fimg3.imgtn.bdimg.com%2Fit%2Fu%3D660414558%2C1171548186%26fm%3D214%26gp%3D0.jpg",
"link": "https://www.19lou.com/creampic-fid-58-t-9331399727863700-p-9741399727863972-nodigest-all-dateline.html",
"title": "图片",
"alt": "图片"
}
]
Banner
body {
margin: 0;
padding: 0;
}
#banner {
position: relative;
margin: 20px auto;
overflow: hidden;
height: 460px;
width: 1200px;
border-radius: 4px;
background-color: white;
cursor: pointer;
background-color: gray;
}
#img-wrap img {
object-fit: cover;
-o-object-fit: cover;
overflow: hidden;
position: absolute;
}
#img-wrap {
overflow: hidden;
position: absolute;
top: 60px;
bottom: 0;
padding: 0;
margin: 0;
}
#btn-menu, #btn-menu li {
list-style: none;
float: left;
margin: 0;
padding: 0;
border: none;
}
#btn-menu li {
line-height: 60px;
font-size: large;
color: #eee;
width: 400px;
text-align: center;
}
#btn-menu .selected {
color: #0ff;
}
#btn-menu li:hover {
background-color: rgba(0, 255, 255, .2);
}
#btn-menu {
width: 100%;
top: 0;
position: absolute;
background-color: rgba(200, 200, 200, .4);
}
.btn {
position: absolute;
width: 40px;
top: 220px;
height: 80px;
background: url(img/arrow.png) rgba(0, 0, 0, 0.05) center no-repeat;
border-radius: 2px;
}
#next {
right: 0;
}
#prev {
transform: rotate(180deg);
left: 0;
}
.btn:hover {
background-color: rgba(255, 255, 255, .2);
}
#mask, #mask-help {
position: absolute;
height: 70px;
text-align: center;
}
.mask-bg {
background-color: #000;
height: 60px;
border-radius: 2px;
}
#mask-help {
left: -100%;
}
.triangle {
display: block;
width: 0;
height: 0;
border-width: 10px 16px 0;
border-style: solid;
border-color: #555 transparent transparent;
top: 60px;
position: absolute;
left: 50%;
margin-left: -16px;
z-index: 999;
}
var intervalId;
$.easing['sin'] = function (v) {
return v * Math.sin(Math.PI / 2);
};
$.getJSON('data/banner.json', (bannerData) => {
$('#prev').click(prev);
$('#next').click(next);
if (!bannerData || bannerData.length < 1) {
return;
}
var speed;
$('#banner').bind('mouseover mouseout', function (event) {
clearInterval(intervalId);
event.type === 'mouseout' && showAndNone();
});
const btnMenu = $('#btn-menu');
btnMenu.html(bannerData.reduce((html, item) => html + '' + item.title + ' ', ''));
const width = btnMenu.width() / bannerData.length;
const btns = btnMenu.children('li').css({width: width}).click(function () {
pics.idx = $(this).index();
moveToIdx();
});
const imgWrap = $('#img-wrap').click(function () {
window.open(bannerData[pics.idx].link, '_blank');
}).css({left: 0, width: bannerData.length + '00%'});
const imgWidth = imgWrap.width() / bannerData.length;
imgWrap.html(bannerData.reduce((html, item) => html + '', ''));
const pics = imgWrap.children('img').css({width: imgWidth}).each(function (i, e) {
$(e).css({left: i * imgWidth})
});
pics.idx = pics.prevIdx = 0;
const mask = $('#mask');
const maskHelp = $('#mask-help');
mask.css({left: 0, right: width * (pics.length - 1)});
maskHelp.css({left: -width, right: imgWidth});
btns.eq(0).addClass('selected');
function setDefaultPicsLeft() {
pics.each((i, e) => {
$(e).css({left: i * imgWidth});
});
}
function prepare() {
maskHelp.stop(true).css({left: -width, right: imgWidth});
mask.stop(true).css({left: width * pics.prevIdx, right: width * (pics.length - 1 - pics.prevIdx)});
speed = Math.sqrt(Math.abs(pics.idx - pics.prevIdx)) * 300;
}
function next() {
++pics.idx;
prepare();
if (pics.idx > pics.length - 1) {
pics.idx = 0;
mask.animate(getAnimate(imgWidth, -width), speed);
maskHelp.animate(getAnimate(0, imgWidth - width), speed);
} else {
maskAnimate();
}
moveToNextOrPrev(true);
}
function getAnimate(left, right) {
return {
left: [left, 'swing'],
right: [right, 'sin']
};
}
function maskAnimate() {
mask.animate(getAnimate(width * pics.idx, width * (pics.length - 1 - pics.idx)), speed);
}
function moveToNextOrPrev(isNext) {
imgWrap.stop(true, true).css({left: -imgWidth}).animate({left: isNext ? -2 * imgWidth : 0}, speed);
pics.eq(pics.prevIdx).css({left: imgWidth}).siblings().css({left: -imgWidth});
pics.eq(pics.idx).css({left: isNext ? 2 * imgWidth : 0});
update();
}
function prev() {
--pics.idx;
prepare();
if (pics.idx < 0) {
pics.idx = pics.length - 1;
mask.animate(getAnimate(0, imgWidth), speed);
maskHelp.css({left: imgWidth, right: -width}).animate(getAnimate(imgWidth - width, 0), speed);
} else {
maskAnimate();
}
moveToNextOrPrev(false);
}
function moveToIdx() {
prepare();
setDefaultPicsLeft();
maskAnimate();
imgWrap.stop(true).css({left: -pics.prevIdx * imgWidth}).animate({left: -imgWidth * pics.idx}, speed);
update();
}
function update() {
btns.eq(pics.prevIdx).removeClass('selected');
btns.eq(pics.idx).addClass('selected');
pics.prevIdx = pics.idx;
}
function showAndNone() {
intervalId = setInterval(next, 3000);
}
showAndNone();
});