【问题标题】:Transition not working on hover过渡在悬停时不起作用
【发布时间】:2018-09-04 14:00:50
【问题描述】:

我不知道,但由于某些原因,转换似乎不起作用。我正在测试这个谷歌浏览器。

[data-title] {
	position: relative;
  margin: 100px;
}

[data-title]:hover:before {
  transform: translate(-50%, 0);
	width: 18px;
	height: 6px;
	left: 50%;
	margin-top: 0px;
	top: 100%;
	opacity: 1;
	pointer-events: auto;
  -webkit-transition: all 0.25s;
  transition: all 0.25s;
	content: '';
	position: absolute;
	z-index: 10;
	box-sizing: border-box;
	border-left: 8px solid transparent;
	border-right: 8px solid transparent;
	border-bottom: 10px solid #00204e;
  
}

[data-title]:hover:after {
	transform: translate(-50%, 0);
	left: calc(50%);
	margin-top: 10px;
	top: 100%;
	opacity: 1;
	pointer-events: auto;
  -webkit-transition: all 0.25s;
  transition: all 0.25s;
	font-weight: normal;
	text-shadow: none;
	background: #00204e;
	border-radius: 4px;
	color: #fff;
	content: attr(data-title);
	padding: 10px;
	position: absolute;
	white-space: normal;
	width: max-content;
	font-size: 12px;
	font-family: 'Helvetica Neue';
	line-height: normal;
	max-width: 150px;
	text-align: left;
	height: auto;
	display: inline-block;
}
<span class="dijitButtonContents" id="saveButton" data-title="Save as draft"><span id="saveButton_label">Save</span></span>

任何人都可以帮助我哪里出错或我错过了什么?

我什至尝试将转换时间设置为 +1 秒,但仍然反映不一样。

【问题讨论】:

  • 但是你没有悬停的:before和:after的样式
  • 是的,因为这是我只在悬停时需要的工具提示。
  • 所以你的代码完全按照你写的。
  • 您要制作什么动画?
  • @Pete 我正在尝试为悬停时出现的工具提示设置动画。

标签: css hover transition mousehover


【解决方案1】:

您没有为原始状态设置任何内容,因此转换不知道从什么开始。如果您只想转换项目的外观 - 例如淡入或淡出,那么您需要执行转换不透明度之类的操作:

[data-title] {
  position: relative;
  margin: 100px;
}

[data-title]:before {
  width: 18px;
  height: 6px;
  left: 50%;
  margin-top: 0px;
  top: 100%;
  opacity: 1;
  content: '';
  position: absolute;
  z-index: 10;
  box-sizing: border-box;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-bottom: 10px solid #00204e;
  transform: translate(-50%, 0);
  opacity: 0;
  transition: opacity 0.5s;
  pointer-events: none;
}

[data-title]:after {
  transform: translate(-50%, 0);
  left: calc(50%);
  margin-top: 10px;
  top: 100%;
  opacity: 1;
  font-weight: normal;
  text-shadow: none;
  background: #00204e;
  border-radius: 4px;
  color: #fff;
  content: attr(data-title);
  padding: 10px;
  position: absolute;
  white-space: normal;
  width: max-content;
  font-size: 12px;
  font-family: 'Helvetica Neue';
  line-height: normal;
  max-width: 150px;
  text-align: left;
  height: auto;
  display: inline-block;
  opacity: 0;
  transition: opacity 0.5s;
  pointer-events: none;
}

[data-title]:hover:before,
[data-title]:hover:after {
  opacity: 1;
  pointer-events: auto;
}
<span class="dijitButtonContents" id="saveButton" data-title="Save as draft"><span id="saveButton_label">Save</span></span>

【讨论】:

    【解决方案2】:

    如果您想将某些东西从一个状态传递到另一个状态 transition,您必须定义这两个状态。这意味着有一个基本样式和一个:hover 样式。

    例如:

    .test {
      width: 100px;
      height: 50px;
      background: red;
      transition: all 2s;
    }
    
    .test:hover {
      height: 100px;
    }
    <div class="test">test</div>

    这是可行的,因为height 属性有一个初始状态。然而这:

    .test {
      width: 100px;
      background: red;
      transition: all 2s;
    }
    
    .test:hover {
      height: 300px;
    }
    <div class="test">test</div>

    这不起作用,因为浏览器没有指定的height 作为初始状态。

    【讨论】:

    • 你能回答我原来的问题,因为我使用了 :before 和 :after 元素需要做哪些改变?
    • 这取决于你想要什么,你有一个transition: all 所以我不知道你想要转换什么。如果要转换所有内容,则需要为每个属性创建基本状态。
    猜你喜欢
    • 1970-01-01
    • 2014-12-15
    • 2018-01-19
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多