Skip to content

feat: 全栈训练营第一次作业 #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
32 changes: 32 additions & 0 deletions css/shuffing.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#marquee {
width: 600px;
height: 300px;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}

.control-btn{
width: 30px;
height: 30px;
line-height: 30px;
cursor: pointer;
position: absolute;
top: 50%;
font-size: 30px;
color: #ffffff;
text-align: center;
background: #3f3f3f;
}
.control-btn.next{
right: 0px;
}
.control-btn.prev{
left: 0;
}
#marquee a img {
width: 100%;
}
Binary file added imgs/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 33 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
<div id="marquee">
<img>
<img>
<img>
</div>
<script src="./libs/jquery.js"></script>
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Index</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/fancyapps/[email protected]/dist/jquery.fancybox.min.css" />
<link rel="stylesheet" href="./css/shuffing.css">
</head>

<body>
<div id="marquee">
<span class="control-btn prev">
&lt;
</span> <a href="./imgs/1.jpg"><img src="./imgs/1.jpg" alt="" /></a>
<span class="control-btn next">
&gt;
</span>
</div>
<script src="./libs/jquery.js"></script>
<script src="https://cdn.jsdelivr.net/gh/fancyapps/[email protected]/dist/jquery.fancybox.min.js"></script>
<script src="./libs/marquee.js"></script>
<script charset="utf-8">
var data = ['./imgs/1.jpg', './imgs/2.jpg', './imgs/3.jpg', './imgs/4.jpg']
var options = {
interval:2000
}
$("#marquee").marquee(data,options)
</script>
</body>

</html>
69 changes: 64 additions & 5 deletions libs/marquee.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
$.fn.marquee = function() {
// 可翻页
// popup when click
// 可以用 fancybox
}
/**
* @param {Array} 轮播图片数据
* @param {Object} 配置参数
*/
$.fn.marquee = function (data, options) {
if (!data instanceof Array) return
if (typeof options !== 'object') return
var count = 0
var timer
var config = {
interval: options.interval || 2000
}
var methods = {
// 自动轮播初始化
init: function () {
timer = setInterval(function () {
count++
methods.render()
}, config.interval)
},
// 更新数据
render: function () {
console.log(count)
count = count % data.length
$('#marquee a').attr('href', data[count]);
$('#marquee a img').attr('src', data[count]);
},
next: function () {
count += 1
this.render()
},
prev: function () {
count = count ? count - 1 : data.length - 1
this.render()
}
}
// 点击下一张
$(".control-btn.next").on('click', function () {
console.log('next')
clearInterval(timer)
methods.next()
})
// 点击上一张
$(".control-btn.prev").on('click', function () {
console.log('prev')
clearInterval(timer)
methods.prev()
})
// 绑定fancybox
$.each($("#marquee a"), function (indexInArray, valueOfElement) {
$(valueOfElement).fancybox()
});
// 鼠标移入暂停
$('#marquee').mouseover(function () {
console.log('鼠标移入暂停播放')
clearInterval(timer)
});
// 鼠标移除开始
$('#marquee').mouseleave(function () {
console.log('鼠标移除开始播放')
methods.init()
});
methods.init()
}