鼠标附着式按钮边框

84 篇文章 0 订阅
28 篇文章 0 订阅

鼠标附着式按钮边框

教程地址原文地址(YouTube)

B站教程原文转载(bilibili)

两个视频的内容相同,第二个为转载

效果图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-omoaci6P-1580786056945)(演示.gif)]

代码区

html

  <a href="#" class="button">Button1</a>
	<a href="#" class="button">Button2</a>
	<a href="#" class="button">Button3</a>

CSS

html, body {
  margin: 0; /* 外边距 */
  padding: 0; /* 内边距 */
  width: 100%; /* 宽度 */
  height: 100%; /* 高度 */
  background: #161616; /* 背景颜色 */
  overflow: hidden; /* 溢出隐藏 */
}
body {
  display: flex; /* 弹性盒模型 */
  justify-content: center; /* 主轴对齐 */
  align-items: center; /* 交叉轴对齐 */
  font-family: sans-serif; /* 字体,非衬线 */
}
/* 光标(圆点),边框 */
.cursor, .follow {
  position: absolute; /* 绝对定位 */
  pointer-events: none; /* 事件穿透 */
  width: 0.4em;
  height: 0.4em;
  transform: translate(-50%, -50%) translate3d(0, 0, 0); /* X,Y移动,开启加速 */
}
.cursor {
  background: red;
  border-radius: 100%; /* 让原点为圆型 */
}
.follow {
  padding: 2rem;
  border: 0.1em solid white;
  transition: all 0.2s ease-out;
  border-radius: 100%;
}
/* 当悬停到a标签时,增加的样式 */
.follow.on-focus {
  transform: translate(-0.55rem, -0.55rem) translate3d(0, 0, 0);
  border: 0.1rem solid white;
  border-radius: 0%;
  padding: 0.46rem;
}
.button {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 1rem 2rem;
  margin: 4rem;
  cursor: pointer; /* 鼠标悬停时样式 */
  text-transform: uppercase; /* 字体大写 */
  font-weight: lighter; /* 比父元素更细 */
  color: gray;
  text-decoration: none; /* 清除下划线 */
}

.button:hover{ /* 悬停时更改颜色 */
  color: #fff;
}

JS

	// 光标消失
	document.body.style.cursor = 'none';
	// 新建元素div
	var cursor = document.createElement('div');
	// 给元素增加class
	cursor.classList.add('cursor');
	// 给body添加子元素
	document.body.appendChild(cursor);

	var follow = document.createElement('div');
	follow.classList.add('follow');
	document.body.appendChild(follow);

	// 新建函数 用于删除
	function move(obj, event) {
		obj.style = '';
		obj.style.top = event.clientY + 'px';
		obj.style.left = event.clientX + 'px';
	}

	if(cursor) {
		// 增加鼠标在元素上的移动事件
		window.addEventListener('mousemove', function(event) {
			var e = event;
			var t= e.target;
			var f = follow;
			var c = cursor;
			// 判断元素标签名为a时进行
			if(t.tagName == 'A') {
				// 清除红点
				c.style.backgroundColor = 'transparent';
				// 给follow设置为当前获取到的元素t的属性
				f.style.top = t.offsetTop + 'px';
				f.style.left = t.offsetLeft + 'px';
				f.style.width = t.clientWidth + 'px';
				f.style.height = t.clientHeight + 'px';
				// 进行class添加
				f.classList.add('on-focus');
			} else {
				// 执行函数-清除
				move(c, e);
				move(f, e);
				// 删除class
				f.classList.remove('on-focus');
			};
		});
	}

教程地址原文地址(YouTube)

B站教程原文转载(bilibili)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值