【问题标题】:Make CSS transition fade out after hovered for long enough在悬停足够长的时间后使 CSS 过渡淡出
【发布时间】:2018-04-15 07:11:02
【问题描述】:

我正在为网站主页制作幻灯片。我使用 CSS 构建了当幻灯片悬停时出现的箭头按钮。但我认为很酷的一个功能是能够在一段时间后鼠标消失,按钮消失。我对使用 JS 或 CSS 的答案很好。这是我的代码:

.master {
	display: none;
	width: 100vw;
}

.mcont {
	background-image: url("../Pictures/magtrans.jpg");
	padding: auto;
	text-align: center;
	width: 100vw;
	height: 100vh;
	background-repeat: no-repeat;
	background-size: 100% 100%;
	background-position: center;
}

.hover {
	height: 100vh;
}

.mtext {
	color: #ffffff;
	display: table;
	background-color: rgba(0, 0, 0, 0.5);
	margin: auto;
	padding: 3vw;
	position: relative;
	top: 50%;
	-webkit-transform: translateY(-50%);
	-ms-transform: translateY(-50%);
	transform: translateY(-50%);
	font-family: helvetica, arial, sans-serif;
}

.mtext h1 {
	padding: 0;
	margin: 0;
	padding-bottom: 1vh;
	font-size: 5vh;
}

.mtext p {
	padding: 0;
	margin: 0;
	padding-bottom: 2.5vh;
	font-weight: bold;
	font-size: 2vh;
}

.mtext button {
	background-color: #5555ff;
	border: none;
	color: #ffffff;
	padding: 15px 32px;
	text-align: center;
	text-decoration: none;
	display: inline-block;
	font-size: 16px;
	outline: none;
	font-weight: bold;
	cursor: pointer;
	-webkit-transition: background-color 2s;
	transition: background-color 2s;
}

.mtext button:hover {
	background-color: #55aaff;
}

.leftc, .rightc {
	border-radius: 50%;
	width: 5vw;
	height: 5vw;
	background-color: rgba(0, 0, 0, 0.5);
	vertical-align: middle;
	border: none;
	cursor: pointer;
	outline: none;
	color: #ffffff;
	font-size: 3vw;
	position: absolute;
	top: 50%;
	-webkit-transform: translateY(-50%);
	-ms-transform: translateY(-50%);
	transform: translateY(-50%);
	font-family: helvetica, arial, sans-serif;
	opacity: 0;
	-webkit-transition: opacity 1s;
	transition: opacity 1s;
}

.hover:hover > .leftc {
	opacity: 1.0;
}

.hover:hover > .rightc {
	opacity: 1.0;
}

.leftc {
	left: 1vw;
}

.rightc {
	right: 1vw;
}
<div class='mcont'>
  <img class='master' src='Media/Pictures/magtrans.jpg'/>
	<div class='hover'>
		<button class='leftc'><</button>
		<button class='rightc'>></button>
		<div class='mtext'>
			<h1>Slideshow</h1>
			<p>Buttons to left and right!</p>
			<button>Learn More</button>
		</div>
	</div>
</div>

我需要的是类似于 Google 幻灯片中的内容,其中光标和工具栏会在几秒钟后消失,并在鼠标移动时返回。我对 CSS 过渡和动画进行了广泛的研究。动画 cursor 可以,但 cursor 不可动画。

【问题讨论】:

  • 即使您正在共享一些代码,它与要求无关,并且您在实现目标方面取得了 0 个进展(并且没有编码尝试)。这使您成为要求免费工作的客户。如果您不想要这种状态,请更改它:(重新)搜索,尝试对其进行编码,询问我们为什么某些 CSS 或 JS 没有按照您的想法执行,即使文档让您相信它应该执行,等等......所以不是免费的编码服务。简而言之,我们只是一群程序员互相传授技巧和编码最佳实践。我们不是来工作的。
  • @AndreiGheorghiu 我彻底研究了 CSS 过渡和动画,但一无所获。但我当然同意,SO 不是免费的编码服务。
  • 那是因为您不能使用 CSS 在鼠标移动时运行代码。您必须为此使用 Javascript。最简单的解决方案是在鼠标移动时添加/删除一个类。
  • 你说得对,在我问 SO 之前,我可能应该研究/尝试过一些 JS。我只是希望我在某个地方错过了一些明显的答案,而且我不会花半个小时来编写 JS 来做一些你可以用三行 CSS 做的事情。

标签: css css-transitions


【解决方案1】:

理论

CSS 状态(可用于根据元素状态设置样式 - 因此基于用户交互)是:

:hover         :active        :focus         :target        :link

为此,您可以添加验证伪类

:checked       :valid         :invalid       :default       :disabled
:empty         :enabled       :optional      :in-range      :out-of-range
:read-only     :read-write    :required      :indeterminate 

您还有结构伪类(:nth-*:first-*:last-*),一些实验性的和杂项:

:scope         :dir           :lang          :root          :fullscreen

...当然还有伪元素 (:before & :after)。

如果您想要任何不能简化为上述组合的东西,那么您就处于 JavaScript 领域,因为您希望根据用户交互进行 DOM 修改,而不是原生(以上)交互。另请注意,并非所有浏览器都实现了上述所有内容。

您的请求仅使用 CSS 是不可行的,因为您希望根据鼠标是否移过过去 Nms 以不同方式显示 :hover 状态。

实践(JS方案)

最简单的解决方案是在父级上存在特定类 (active) 时显示箭头。在mousemove 事件上将该类添加到父类,并按所需的时间间隔去抖动函数的执行(删除类)。如果在去抖动期间发生mousemove,则取消去抖动函数并创建它的全新实例。在下面的示例中,去抖动间隔为1s

let deactivate = debounce(function() {
  document.querySelector('.mcont').classList.remove('active');
}),
 activate = function() {
  document.querySelector('.mcont').classList.add('active');
  deactivate();
} 


function debounce(func, wait = 1000) {
  let timeout;
  return function(...args) {
    clearTimeout(timeout);
    timeout = setTimeout(() => {
      func.apply(this, args);
    }, wait);
  };
}

测试:

let deactivate = debounce(function() {
  document.querySelector('.mcont').classList.remove('active');
}),
 activate = function() {
  document.querySelector('.mcont').classList.add('active');
  deactivate();
} 


function debounce(func, wait = 1000) {
  let timeout;
  return function(...args) {
    clearTimeout(timeout);
    timeout = setTimeout(() => {
      func.apply(this, args);
    }, wait);
  };
}
.master {
	display: none;
	width: 100vw;
}

.mcont {
	padding: auto;
	text-align: center;
	width: 100vw;
	height: 100vh;
	background-repeat: no-repeat;
	background-size: 100% 100%;
	background-position: center;
}

.hover {
	height: 100vh;
}

.mtext {
	color: #ffffff;
	display: table;
	background-color: rgba(0, 0, 0, 0.5);
	margin: auto;
	padding: 3vw;
	position: relative;
	top: 50%;
	-webkit-transform: translateY(-50%);
	-ms-transform: translateY(-50%);
	transform: translateY(-50%);
	font-family: helvetica, arial, sans-serif;
}

.mtext h1 {
	padding: 0;
	margin: 0;
	padding-bottom: 1vh;
	font-size: 5vh;
}

.mtext p {
	padding: 0;
	margin: 0;
	padding-bottom: 2.5vh;
	font-weight: bold;
	font-size: 2vh;
}

.mtext button {
	background-color: #5555ff;
	border: none;
	color: #ffffff;
	padding: 15px 32px;
	text-align: center;
	text-decoration: none;
	display: inline-block;
	font-size: 16px;
	outline: none;
	font-weight: bold;
	cursor: pointer;
	-webkit-transition: background-color 2s;
	transition: background-color 2s;
}

.mtext button:hover {
	background-color: #55aaff;
}

.leftc, .rightc {
	border-radius: 50%;
	width: 5vw;
	height: 5vw;
	background-color: rgba(0, 0, 0, 0.5);
	vertical-align: middle;
	border: none;
	cursor: pointer;
	outline: none;
	color: #ffffff;
	font-size: 3vw;
	position: absolute;
	top: 50%;
	-webkit-transform: translateY(-50%);
	-ms-transform: translateY(-50%);
	transform: translateY(-50%);
	font-family: helvetica, arial, sans-serif;
	opacity: 0;
	-webkit-transition: opacity 1s;
	transition: opacity 1s;
}

.active .hover > .leftc {
	opacity: 1.0;
}

.active .hover > .rightc {
	opacity: 1.0;
}

.leftc {
	left: 1vw;
}

.rightc {
	right: 1vw;
}
body {
  margin: 0;
  padding: 0;
}
<div class='mcont' onmousemove="activate()">
  <img class='master' src />
	<div class='hover'>
		<button class='leftc'>&lt;</button>
		<button class='rightc'>></button>
		<div class='mtext'>
			<h1>Slideshow</h1>
			<p>Buttons to left and right!</p>
			<button>Learn More</button>
		</div>
	</div>
</div>

注意:上述原版debounce() 的道具转到Vanilla debounce。如果您需要它们,您可以尝试更高级的版本:

【讨论】:

  • 该表非常有用,而且该解决方案的效果方式比我对 Javascript 解决方案的可怜尝试要好。
【解决方案2】:

我认为关键帧就足够了:

.hover:hover {
 animation-name: slideshowcursor;
 animation-duration: .5s;
 animation-delay: 2s; //that period
 animation-fill-mode: forwards
}
.hover:hover > .rightc, .hover:hover > .leftc {
 animation-name: slideshowarrows;
 animation-duration: .5s;
 animation-delay: 2s; //that period
 animation-fill-mode: forwards
}
@keyframes slideshowcursor {
 0% {
  height: 100vh;
 }
 100% {
  cursor: none;
 }
 }

@keyframes slideshowarrows {
 0% {
  border-radius: 50%;
width: 5vw;
height: 5vw;
background-color: rgba(0, 0, 0, 0.5);
vertical-align: middle;
border: none;
cursor: pointer;
outline: none;
color: #ffffff;
font-size: 3vw;
position: absolute;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
font-family: helvetica, arial, sans-serif;
opacity: 0;
-webkit-transition: opacity 1s;
transition: opacity 1s;
 }
 100% {
  display: none;
 }
 }

注意:我现在在手机上,所以我的测试是有限的,但理论上应该可以工作。

【讨论】:

  • cursor 是否可动画化?
  • 光标本身是不可动画的,但 'cursor: none' 应该可以正常工作
  • 但你可以在某处下载 .gif 光标并执行此操作 'cursor: url("your-image.gif")'
  • 光标闪烁并重新出现。
  • 其实我不知道这怎么可能,我现在正在测试它,它工作正常,它只是消失并一直消失。你在 .hover 的幻灯片光标 CSS 中有 0% 吗??
猜你喜欢
  • 2012-10-11
  • 2012-07-25
  • 2013-10-29
  • 2011-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多