banner.vue 6.9 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<template>
	<div class="banner">
		<div class="slider">
			<span class="slider-btn slider-btn-left"></span>
			<span class="slider-btn slider-btn-right"></span>
			<div class="slider-group">
				<template v-for="item in images">
					<div class="slider-item" :key="item">
						<img :src="item" />
					</div>
				</template>
			</div>
		</div>
	</div>
</template>
<script>
D
DCloud_LXH 已提交
17 18
	var sliderTime;

D
DCloud_LXH 已提交
19 20 21 22
	export default {
		data() {
			return {
				images: [
D
DCloud_LXH 已提交
23 24 25 26 27
					'https://web-assets.dcloud.net.cn/unidoc/zh/case6.png',
					'https://web-assets.dcloud.net.cn/unidoc/zh/case2.png',
					'https://web-assets.dcloud.net.cn/unidoc/zh/case1.png',
					'https://web-assets.dcloud.net.cn/unidoc/zh/case3.png',
					'https://web-assets.dcloud.net.cn/unidoc/zh/case4.png',
D
DCloud_LXH 已提交
28 29 30 31 32 33
				],
			};
		},
		mounted() {
			this.StartBanner();
		},
D
DCloud_LXH 已提交
34 35 36
		beforeDestroy() {
			clearInterval(sliderTime);
		},
D
DCloud_LXH 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
		methods: {
			StartBanner() {
				var banner = document.querySelector('.banner'),
					slider = document.querySelector('.slider-group'),
					sliderItems = document.querySelectorAll('.slider-item'),
					sliderLength = sliderItems.length;
				var bannerWidth = null,
					index = 1; //slider下标
				var arrowLeft = document.querySelector('.slider-btn-left'),
					arrowRight = document.querySelector('.slider-btn-right');

				//创建dots
				var sliderDots = document.createElement('div');
				sliderDots.className = 'slider-dots';
				var dots = '<span class="dot active" data-num="0"></span>';
				for (var i = 1; i < sliderLength; i++) {
					dots += '<span class="dot" data-num="' + i + '"></span>';
				}
				sliderDots.innerHTML = dots;
				document.querySelector('.slider').appendChild(sliderDots);

				//在第一个前、最后一个后各加一个item
				var last_item = document.createElement('div'),
					next_item = document.createElement('div');
				last_item.className = 'slider-item';
				last_item.innerHTML = sliderItems[sliderItems.length - 1].innerHTML;
				slider.insertBefore(last_item, sliderItems[0]);
				next_item.className = 'slider-item';
				next_item.innerHTML = sliderItems[0].innerHTML;
				slider.appendChild(next_item);

				sliderItems = document.querySelectorAll('.slider-item');

				function setSlider() {
					bannerWidth = banner.offsetWidth;
					for (var i = 0, length = sliderItems.length; i < length; i++) {
						sliderItems[i].style.width = bannerWidth + 'px';
					}
					slider.style.width = sliderItems.length * bannerWidth + 'px';
					slider.style.transition = 'transform 0ms';
					slider.style.transform = 'translate(' + -index * bannerWidth + 'px, 0px)';
				}
				setSlider();
				window.onresize = setSlider;
D
DCloud_LXH 已提交
81
				sliderTime = setInterval(sliderStart, 5000);
D
DCloud_LXH 已提交
82 83 84 85

				function sliderStart() {
					//开始轮播)
					/* if(!~location.pathname.indexOf($docsify.banner)){
D
DCloud_LXH 已提交
86 87 88
							clearInterval(sliderTime);
							return;
					} */
D
DCloud_LXH 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
					index += 1;
					if (index < 0 || index > sliderLength + 1) {
						index = index < 0 ? 0 : sliderLength + 1;
					}
					slider.style.transition = 'transform 300ms';
					document.querySelector('.active.dot').classList.remove('active');
					var dotIndex = index > sliderLength ? 0 : index < 1 ? sliderLength - 1 : index - 1;
					sliderDots.children[dotIndex].classList.add('active');
					slider.style.transform = 'translate(' + -index * bannerWidth + 'px, 0px)';
					setTimeout(function () {
						if (index > sliderLength || index < 1) {
							index = index > sliderLength ? 1 : sliderLength;
							slider.style.transition = 'transform 0ms';
							slider.style.transform = 'translate(' + -index * bannerWidth + 'px, 0px)';
						}
					}, 300);
				}
				var mouseClearSlider = false;
				sliderDots.addEventListener('mouseover', function (e) {
					showArrow();
					if (e.target.tagName === 'SPAN') {
						mouseClearSlider = true;
						clearInterval(sliderTime);
						if (e.target.className === 'dot') {
							slider.style.transition = 'transform 300ms';
							document.querySelector('.active.dot').classList.remove('active');
							e.target.classList.add('active');
							index = Number(e.target.dataset.num) + 1;
							slider.style.transform = 'translate(' + -index * bannerWidth + 'px, 0px)';
						}
						return;
					}
					if (mouseClearSlider) {
						mouseClearSlider = false;
						clearInterval(sliderTime);
						sliderTime = setInterval(sliderStart, 5000);
					}
				});
				sliderDots.addEventListener('mouseout', function (e) {
					hideArrow();
					if (mouseClearSlider) {
						mouseClearSlider = false;
						clearInterval(sliderTime);
						sliderTime = setInterval(sliderStart, 5000);
					}
				});
				// 处理 banner 的两个箭头
				document.querySelector('.slider').addEventListener('mouseover', showArrow);
				document.querySelector('.slider').addEventListener('mouseout', hideArrow);
				arrowLeft.addEventListener('mouseover', showArrow);
				arrowLeft.addEventListener('mouseout', hideArrow);
				arrowRight.addEventListener('mouseover', showArrow);
				arrowRight.addEventListener('mouseout', hideArrow);

				function showArrow() {
					//显示箭头
					arrowLeft.style.display = 'block';
					arrowRight.style.display = 'block';
					// 		var scroll_left = document.documentElement.scrollLeft;
					// 		arrowLeft.style.left = scroll_left + "px";
					// 		arrowRight.style.right = document.documentElement.scrollWidth - document.documentElement.clientWidth - scroll_left +
					// 			'px';
				}

				function hideArrow() {
					//隐藏箭头
					arrowLeft.style.display = 'none';
					arrowRight.style.display = 'none';
				}
				arrowLeft.addEventListener('click', function () {
					index -= 2;
					clearInterval(sliderTime);
					sliderStart();
					sliderTime = setInterval(sliderStart, 5000);
				});
				arrowRight.addEventListener('click', function () {
					clearInterval(sliderTime);
					sliderStart();
					sliderTime = setInterval(sliderStart, 5000);
				});
			},
		},
	};
</script>
D
DCloud_LXH 已提交
173
<style>
D
DCloud_LXH 已提交
174 175 176 177 178 179 180
	/* banner */
	.banner {
		width: 100%;
		margin: 0 auto;
		overflow: hidden;
	}

D
DCloud_LXH 已提交
181
	.banner .slider {
D
DCloud_LXH 已提交
182 183 184 185 186
		position: relative;
		width: 100%;
		overflow: hidden;
	}

D
DCloud_LXH 已提交
187
	.banner .slider-btn {
D
DCloud_LXH 已提交
188 189 190 191 192 193 194 195 196
		position: absolute;
		cursor: pointer;
		width: 41px;
		height: 69px;
		top: 50%;
		transform: translateY(-50%);
		-webkit-transform: translateY(-50%);
		z-index: 20;
		display: none;
D
DCloud_LXH 已提交
197
		background-image: url(https://web-assets.dcloud.net.cn/unidoc/zh/arrow.png);
D
DCloud_LXH 已提交
198 199
	}

D
DCloud_LXH 已提交
200
	.banner .slider-btn.slider-btn-left {
D
DCloud_LXH 已提交
201 202 203
		left: 0;
	}

D
DCloud_LXH 已提交
204
	.banner .slider-btn.slider-btn-right {
D
DCloud_LXH 已提交
205 206 207 208
		right: 0;
		background-position-x: 41px;
	}

D
DCloud_LXH 已提交
209
	.banner .slider-item {
D
DCloud_LXH 已提交
210 211 212 213
		float: left;
		width: 100%;
	}

D
DCloud_LXH 已提交
214
	.banner .slider .slider-dots {
D
DCloud_LXH 已提交
215 216 217 218 219 220 221 222 223 224 225
		display: none;
		position: absolute;
		line-height: 0;
		left: 50%;
		bottom: 5px;
		transform: translateX(-50%);
		-webkit-transform: translateX(-50%);
		text-align: center;
		font-size: 0;
	}

D
DCloud_LXH 已提交
226
	.banner .slider .dot {
D
DCloud_LXH 已提交
227 228 229 230 231 232 233 234 235
		display: inline-block;
		margin: 0 5px;
		width: 10px;
		height: 10px;
		border-radius: 50%;
		background: #ccc;
		cursor: pointer;
	}

D
DCloud_LXH 已提交
236
	.banner .slider .dot.active {
D
DCloud_LXH 已提交
237 238 239 240 241
		width: 30px;
		border-radius: 10px;
		background: rgb(225, 114, 0);
	}
</style>