JavaScript基础(十四)淘宝banner、京东banner、弹窗组件

淘宝banner、京东banner、弹窗组件

淘宝无缝Banner

*{padding:0;margin: 0;}
body{background:gray;}
ul li{list-style: none;}
#banner{width: 520px;height: 280px;margin: 50px auto;position: relative;overflow: hidden;box-shadow: 0 0 1em pink;}
#banner:hover .btn div{display: block;}
/*图片*/
#banner .pic ul{width: 1000%;margin-left: -520px;}
#banner .pic li{float: left;}
#banner .pic li img{cursor: pointer;}
/*图片切换按钮*/
#banner .tab ul{width: 72px;height: 14px;background: rgba(255, 255, 255, .3);position: absolute;bottom: 10px;left: 50%;margin-left: -36px;border-radius: 7px;}
#banner .tab li{float: left;width: 10px;height: 10px;background: #ccc;border-radius: 50%;margin: 2px;cursor: pointer;}
#banner .tab li.on{background: #f60;}
/*左右按钮*/
#banner .btn div{width: 24px;height: 36px;background: rgba(0,0,0,.3);position: absolute;font-size: 24px;color: #e5e5e5;font-weight: bold;text-align: center;line-height: 36px;top:50%;margin-top: -18px;cursor: pointer;display: none;}
#banner .btn div:hover{background: rgba(0, 0, 0, .6);}
#banner .btn .left-btn{left: 0}
#banner .btn .right-btn{right: 0}
<div id="banner">
	<div class="pic">
		<ul>
			<li><img src="img/taobao/5.jpg"/></li>
			<li><img src="img/taobao/1.jpg"/></li>
			<li><img src="img/taobao/2.jpg"/></li>
			<li><img src="img/taobao/3.jpg"/></li>
			<li><img src="img/taobao/4.jpg"/></li>
			<li><img src="img/taobao/5.jpg"/></li>
			<li><img src="img/taobao/1.jpg"/></li>
		</ul>
	</div>
	<div class="tab">
		<ul><li class="on"></li><li></li><li></li><li></li><li></li></ul>
	</div>
	<div class="btn">
		<div class="left-btn">&lt;</div>
		<div class="right-btn">&gt;</div>
	</div>
</div>
$(function(){
	var $banner = $('#banner');
	var $tabLis = $('#banner .tab li');
	var imgWidth = $('#banner .pic li').width();
	var imgConut = $tabLis.length;
	var $imgUl = $('#banner .pic ul');
	var $btnDiv = $('#banner .btn div');
	var imgIndex = 0;
	var timer;
	var nowTime = 0;
	// 图片切换 
	$tabLis.click(function(){
		var index = $(this).index();
		imgIndex = index;		
		$(this).addClass('on').siblings().removeClass('on');
		$imgUl.animate({marginLeft: -imgWidth*(imgIndex+1)}, 500);
	});
	// 左右按钮切换
	$btnDiv.click(function(){
		if(new Date().getTime() - nowTime > 600){
			nowTime = new Date().getTime();
			var index = $(this).index();
			if(index == 0){ // 左边按钮
				imgIndex--;
			}else{ // 右边按钮
				imgIndex++;
			}
			fn();
		}
	}).mousedown(function(){
		return false;
	});
	$banner.hover(function(){
		clearInterval(timer);
	},function(){
		auto();
	});
	auto();
	// 自动轮播
	function auto(){
		timer = setInterval(function(){
			imgIndex++;
			fn();
		}, 1000);
	}
	// 轮播切换
	function fn(){
		var tmpIndex = imgIndex;
		if(tmpIndex == imgConut){
			tmpIndex = 0;
		}
		if(tmpIndex < 0){
			tmpIndex = imgConut-1;
		}
		$tabLis.eq(tmpIndex).addClass('on').siblings().removeClass('on');
		$imgUl.animate({marginLeft: -imgWidth*(imgIndex+1)}, 500, function(){
			if(imgIndex == imgConut){
				$imgUl.css({marginLeft: -imgWidth});
				imgIndex = 0;
			}
			if(imgIndex < 0){
				$imgUl.css({marginLeft: -imgWidth*imgConut});
				imgIndex = imgConut - 1;
			}
		});
	}
});

京东淡入淡出Banner

*{padding:0;margin: 0;}
body{background:gray;}
ul li{list-style: none;}
#banner{width: 730px;height: 454px;margin: 50px auto;position: relative;box-shadow: 0 0 1em pink;}
#banner:hover .btn div{display: block;}
/*图片*/
#banner .pic li{position: absolute;display: none;}
#banner .pic li img{cursor: pointer;}
/*图片切换按钮*/
#banner .tab ul{width: 132px;height: 22px;position: absolute;bottom: 10px;left: 50%;margin-left: -66px;}
#banner .tab li{width: 18px;height: 18px;background: #666;border-radius: 50%;margin: 2px;text-align: center;line-height: 18px;cursor: pointer;color: #e5e5e5;float: left;font-weight: bold;}
#banner .tab li.on{background: #b61b1f;}
/*左右按钮*/
#banner .btn div{width: 28px;height: 62px;background: rgba(0, 0, 0, .3);text-align: center;line-height: 62px;position: absolute;top: 50%;margin-top: -31px;font-size: 26px;color: #fff;display: none;cursor: pointer;}
#banner .btn .left-btn{left: 10px;}
#banner .btn .right-btn{right: 10px;}
<div id="banner">
	<div class="pic">
		<ul>
			<li style="display: block;"><img src="img/jd/1.jpg"/></li>
			<li><img src="img/jd/2.jpg"/></li>
			<li><img src="img/jd/3.jpg"/></li>
			<li><img src="img/jd/4.jpg"/></li>
			<li><img src="img/jd/5.jpg"/></li>
			<li><img src="img/jd/6.jpg"/></li>
		</ul>
	</div>
	<div class="tab">
		<ul><li class="on">1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul>
	</div>
	<div class="btn">
		<div class="left-btn">&lt;</div>
		<div class="right-btn">&gt;</div>
	</div>
</div>
$(function(){
	var $banner = $('#banner');
	var $tabLis = $('#banner .tab li');
	var $imgLis = $('#banner .pic li');
	var $btnDiv = $('#banner .btn div');
	var imgConut = $tabLis.length;
	var index = 0;
	var timer;
	var nowTime = 0;

	$tabLis.hover(function(){
		index = $(this).index();
		fn();
	});
	$btnDiv.click(function(){
		if(new Date().getTime() - nowTime > 800){
			nowTime = new Date().getTime();
			var i = $(this).index();
			if(i == 0){ // 左边按钮
				index--;
				if(index<0){
					index = imgConut-1;
				}
			}else{ // 右边按钮
				index++;
				index %= imgConut;
			}
			fn();
		}
	}).mousedown(function(){
		return false;
	});
	$banner.hover(function(){
		clearInterval(timer);
	},function(){
		auto();
	});
	auto();
	// 自动播放
	function auto(){
		timer = setInterval(function(){
			index++;
			index %= imgConut;
			fn();
		}, 1000);
	}
	// 图片切换
	function fn(){
		$tabLis.eq(index).addClass('on').siblings().removeClass('on');
		$imgLis.eq(index).stop(true).fadeIn('slow').siblings().stop(true).fadeOut('slow');
	}
});

弹窗组件

/*dialog.css*/
.dialog{width: 640px;height: 480px;box-shadow:  0 0 1em #ddd;position:fixed;top: 10px;left: 10px;z-index: 2;background: #fff;}
.dialog .header{height: 40px;line-height: 40px;border-bottom: 1px solid #ddd;padding-left: 15px;cursor: move;}
.dialog .header span{font-weight: bold;}
.dialog .content{padding: 10px 15px;height: calc(100% - 110px);overflow: auto;}
.dialog .button{height: 40px;line-height: 40px;text-align: center;position: absolute;bottom: 0;right: 10px;display: flex;}
.dialog .button a{text-decoration: none;display: block;background: #f4f4f4;width: 90px;height: 30px;line-height: 30px;border: 1px solid #ddd;border-radius: 4px;color: #fff; cursor: pointer;margin-left: 10px}
.dialog .button .confirm{background: #24b8f3;}
.dialog .button .confirm:hover{background: #2d78f4;}
.dialog .button .cancel{background: #fff; color: #333;}
.dialog .button .cancel:hover{border-color: #2d78f4;background: #eee;}
.overlay{position: fixed;top:0;left: 0;width: 100%;height: 100%;z-index: 1;background: rgba(0, 0, 0, .3);}
.dialog .header .close{float:right;text-decoration:none;margin-right:10px;color:#000;opacity:0;transition:all .3s ease;}
.dialog:hover .close{opacity:1;transition:all .3s ease;transform:rotate(360deg);}
// dialog.js
(function($){
	var $el = null;
	var $options = {
		width: 640,
		height: 480,
		top: 200,
		title: '标题',
		content: 'Hello, world!',
		dragable: true
	}
	$.dialog = {
		init: function(options){
			$options = $.extend({}, $options, options);
			$el = this.template();
			if($options.dragable){
				this._drag();
			}
			this._style();
			this._resize();
			this._event();
		},
		template: function(title){
			var sb='<div class="dialog">';
				sb+='	<div class="header">';
				sb+='		<span>'+$options.title+'</span>';
				sb+='		<a href="javascript:void(0);" class="close"><i class="iconfont">&#xe607;</i></a>';
				sb+='	</div>';
				sb+='	<div class="content">';
				sb+='		' + $options.content;
				sb+='	</div>';
				sb+='	<div class="button">';
				sb+='		<a href="javascript:void(0);" class="confirm">确定</a>';
				sb+='		<a href="javascript:void(0);" class="cancel">取消</a>';
				sb+='	</div>';
				sb+='</div>';
			var $el = $(sb)
			$('body').append($el).append('<div class="overlay"></div>');
			return $el;
		},
		// 时时改变弹出层位置
		_resize: function(){
			var _this = this;
			$(window).resize(function(){
				_this._position();
			})
		},
		// 标题拖拽
		_drag: function(){
			$el.find('.header').mousedown(function(ev){
				if($options.dragable){
					var e = ev || event;
					var x = e.clientX - $el.offset().left;
					var y = e.clientY - $el.offset().top;
					$(document).mousemove(function(ev){
						var e = ev || event;
						var left = e.clientX - x;
						var top = e.clientY - y;
						// var maxL = $(window).width() - $el.width();// $el.width()不包括边框
						var maxL = $(window).width() - $el.outerWidth();
						// var maxT = $(window).height() - $el.height();
						var maxT = $(window).height() - $el.outerHeight();
						if(left<0) left = 0;
						if(top<0) top = 0;
						if(left>maxL) left = maxL;
						if(top>maxT) top = maxT;
						$el.css({left: left, top: top});
					});
				}
				$(document).mouseup(function(){
					$(document).off('mousemove').off('mouseup');
				}).mousedown(function(){
					return false;
				});
			});
		},
		// 样式修改
		_style: function(){
			var minWidth = 300;
			var minHeight = 150;
			var maxWidth = $(window).width() - 40;
			var maxHeight = $(window).height() - 40;
			var width = $options.width<minWidth ? minWidth :$options.width;
			var height = $options.height < minHeight ? minHeight :$options.height;
			width = $options.width>maxWidth ? maxWidth : $options.width;
			height = $options.height>maxHeight ? maxHeight : $options.height;
			$el.css({
				width: width,
				height: height,
				background: $options.background,
				border: $options.border,
				opacity: $options.opacity,
				zIndex: $options.zIndex
			});
			// 定位
			var top = $options.top ? $options.top : ($(window).height() - $el.outerHeight())/2;
			var left = $options.left ? $options.left : ($(window).width() - $el.outerWidth())/2;
			$el.css({top: top, left: left})
		},
		// 触发事件
		_event: function(){
			// 确认
			$el.find('.confirm').click(function(){
				if($options.callback){
					$options.callback.call($el, {type: 'confirm'});
				}
				$el.remove();
				$('body .overlay').remove();
			});
			// 取消
			$el.find('.cancel, .close').click(function(){
				if($options.callback){
					$options.callback.call($el, {type: 'cancel'});
				}
				$el.remove();
				$('body .overlay').remove();
			});
		}
	}
})(jQuery);

测试

<style type="text/css">
*{padding:0;margin: 0;}
.container{margin: 50px;}
</style>
<link href="dialog/iconfont/iconfont.css" rel="stylesheet"/>
<link href="dialog/css/dialog.css" rel="stylesheet"/>

<div class="container">
	弹窗组件
	<input type="button" value="点击触发" id="btn">
</div>
<script type="text/javascript" src="dialog/js/jquery-1.12.2.min.js"></script>
<script type="text/javascript" src="dialog/js/dialog.js"></script>
<script type="text/javascript">
	$(function(){
		$('#btn').click(function(){
			$.dialog.init({
				width: 300,
				height: 150,
				top: 200,
				title: 'Hello',
				content: '你好',
				// dragable: false,
				callback: function(options){
					console.log(this, options)
				}
			});
		})
	});
</script>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

讲文明的喜羊羊拒绝pua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值