炫酷按钮一些简单的js加heml和css实现炫酷的按钮

先来看一下效果

炫酷按钮一些简单的js加heml和css实现炫酷的按钮_第1张图片

按钮周围的的一个跑马灯效果!

跑马灯效果就是四个span标签一直再做一个运动
炫酷按钮一些简单的js加heml和css实现炫酷的按钮_第2张图片
给父元素设置overflow: hidden;后span移出div的时候就会隐藏移出的部分
炫酷按钮一些简单的js加heml和css实现炫酷的按钮_第3张图片

鼠标移上去跑马灯效果隐藏

炫酷按钮一些简单的js加heml和css实现炫酷的按钮_第4张图片
这是html,可以看到所谓跑马灯就是四个span的移动所形成。


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
    <style>
        * {
      
            margin: 0;
            padding: 0;
        }
		body{
      
			background-color: #040404;
			 overflow: hidden;
		}
        div{
      
			width: 100px;
			height: 50px;
			margin: 20px;
			overflow: hidden;
			position: relative;
		}
		div:hover{
      
			background: #00FFFF;
			border-radius: 5px;
			color: #fff;
			box-shadow: 0 0 5px #03e9f4, 0 0 25px #03e9f4, 0 0 50px #03e9f4, 0 0 100px #03e9f4;
			
		}
		button{
      
			border: none;
			background-color: transparent;
			line-height: 50px;
			text-indent: 25px;
			font-size: 25px;
			text-align: center;
			font-weight: bold;
			color: #FFFFFF;
			outline: none;
			/* color: transparent; */
		}
        span{
      
            background-color: #00FFFF;
        }
		.one{
      
			position: absolute;
			width: 40px;
			height: 3px;
			top: 0;
			left: -40px;
			background: -webkit-linear-gradient(left, transparent, #03e9f4);
			border-radius: 3px 0 0 3px;
		}
		.two{
      
			position: absolute;
			width: 3px;
			height: 40px;
			top: -40px;
			right: 0;
			background: -webkit-linear-gradient(top, transparent, #03e9f4);
			border-radius: 3px 0 0 3px;
		}
		.three{
      
			position: absolute;
			width: 40px;
			height: 3px;
			bottom: 0;
			left: 100px;
			background: -webkit-linear-gradient(left, #03e9f4, transparent);
			border-radius: 3px 0 0 3px;
		}
		.four{
      
			position: absolute;
			width: 3px;
			height: 40px;
			top: 55px;
			left: 0;
			background: -webkit-linear-gradient(top, #03e9f4, transparent);
			border-radius: 3px 0 0 3px;
		}
    style>
head>
<body>
<div id="box">
	<button type="button">登录button>
	<span class="one">span>
	<span class="two">span>
	<span class="three">span>
	<span class="four">span>
div>
body>

html>

这里是js代码

<script>
	let oSpan = document.querySelectorAll("span");
	let oDiv = document.querySelector("#box");

	function fun(element){
     
		let s = null;
		if(element == oSpan[0] || element == oSpan[1]){
     
			s = 5;
		}else{
     
			s = -5;
		}
		if(element == oSpan[0] || element == oSpan[2]){
     
			let time = setInterval(function(){
     
				element.style.left = element.offsetLeft + s + "px";
				if( oSpan[0] == element && element.offsetLeft > oDiv.offsetWidth + 10){
     
					element.style.left = - 40 + "px";
					clearInterval(time);
				}else if(element.offsetLeft < -40 && element == oSpan[2]){
     
					element.style.left = oDiv.offsetWidth + "px";
					clearInterval(time);
				}
			},20);
		}else{
     
			let time = setInterval(function(){
     
				element.style.top = element.offsetTop + s + "px";
				if(element.offsetTop > oDiv.offsetHeight + 15 && element == oSpan[1]){
     
					element.style.top = - 40 + "px";
					clearInterval(time);
				}else if(element.offsetTop < -40 && element == oSpan[3]){
     
					element.style.top = oDiv.offsetHeight + "px";
					clearInterval(time);
				}
			},20)
		}
	}
	function fun1(){
     
		fun(oSpan[0]);
		setTimeout(function(){
     
			fun(oSpan[1]);
		},400);
		setTimeout(function(){
     
			fun(oSpan[2]);
		},600);
		setTimeout(function(){
     
			fun(oSpan[3]);
		},1000);
	}
	fun1();
	setInterval(function(){
     
		fun1();
	},1200);
</script>

你可能感兴趣的:(前端入门,html,css3,javascript)