js 实现 toast

点击页面空白地方试试

.bw-toast{
    box-sizing: border-box;
    position: fixed;
    right: calc(50% - 200px);
    height: 36px;
    width: 400px;
    border-radius: 3px;
    color: #fff;
    font-size: 16px;
    line-height: 1;
    padding: 10px 10px;
    background-color: #000;
    transition: all .1s;
}

  

var bw = {};
bw.list = [];
bw.toast = function( txt ){
	var i = 38;
	if( bw.list.length ){
		bw.list.forEach( function(element, index) {
			element.setAttribute('style', 'bottom: '+(bw.list.length-index)*i+'px;');
		});
	}
	var toast = document.createElement('div');
	toast.classList = 'bw-toast';
	toast.setAttribute('style', 'bottom: -36px;');

	var msg = document.createTextNode(txt);
	toast.append(msg);
	bw.list.push(toast);
	document.body.append(toast);

	setTimeout(function(){
		toast.setAttribute('style', 'bottom: 0px;');
	}, 0)
	setTimeout(function(){
		toast.parentNode.removeChild(toast);
		bw.list.shift();
	}, 2000)
}

document.body.onclick = function(){
	bw.toast(new Date())
}

  

转载于:https://www.cnblogs.com/luozhangshuai/p/8109322.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值