html - 如何在鼠标悬停时添加淡入淡出效果

标签 html css wordpress mouseover fade

我正在使用它来创建鼠标悬停效果:

<a href="http://glim.pt/produtos/cadeiras">
    <img src="http://glim.pt/wp-content/uploads/2017/02/cadeiras_categoria-300x300.png"
     onmouseover=" this.src='http://glim.pt/wp-content/uploads/2017/02/cadeiras_cat2-300x300.png';"
     onmouseout=" this.src='http://glim.pt/wp-content/uploads/2017/02/cadeiras_categoria-300x300.png';">
    </img>
</a>

但是我想要它是平滑的,我该如何添加淡入淡出效果呢?它用于 Wordpress 页面。

最佳答案

如果您不反对它,您可以使用 CSS 转换来完成此操作,详见 in this blog post on crossfading images :

/* A wrapper for your images to transition */
.transition-wrapper {
  position:relative;
  height:300px;
  width:300px;
  margin:0 auto;
}

/* Position each image and apply a transition */
.transition-wrapper img {
  position:absolute;
  left:0;
  -webkit-transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -o-transition: opacity 1s ease-in-out;
  transition: opacity 1s ease-in-out;
}

/* Automatically hide an image during hover (to reveal the other one) */
.transition-wrapper img:last-of-type:hover {
  opacity:0;
}

然后相应地更新您的标记:

<a class='transition-wrapper' href="http://glim.pt/produtos/cadeiras"> 
  <img src='http://glim.pt/wp-content/uploads/2017/02/cadeiras_cat2-300x300.png' />
  <img src='http://glim.pt/wp-content/uploads/2017/02/cadeiras_categoria-300x300.png' />
</a>

示例

enter image description here

/* A wrapper for your images to transition */
.transition-wrapper {
  position:relative;
  height:300px;
  width:300px;
  margin:0 auto;
}

/* Position each image and apply a transition */
.transition-wrapper img {
  position:absolute;
  left:0;
  -webkit-transition: opacity 1s ease-in-out;
  -moz-transition: opacity 1s ease-in-out;
  -o-transition: opacity 1s ease-in-out;
  transition: opacity 1s ease-in-out;
}

/* Automatically hide an image during hover (to reveal the other one) */
.transition-wrapper img:last-of-type:hover {
  opacity:0;
}
<a class='transition-wrapper' href="http://glim.pt/produtos/cadeiras"> 
  <img src='http://glim.pt/wp-content/uploads/2017/02/cadeiras_cat2-300x300.png' />
  <img src='http://glim.pt/wp-content/uploads/2017/02/cadeiras_categoria-300x300.png' />
</a>

关于html - 如何在鼠标悬停时添加淡入淡出效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42072880/

相关文章:

html - Css 边框三 Angular 形

javascript - 如何将 jquery 添加到 wordpress 插件

php - 在 Wordpress 页面上设置单独元素的样式

javascript - 未捕获的类型错误 : $ is not a function bold automatic the tabele from javascript

javascript - 如何创建多个图像预览

html - 如何在 Bootstrap 中垂直居中容器?

html - CSS - 无论设备的屏幕尺寸如何,都将元素定位在同一位置

在循环中添加的元素的 jQuery 宽度

html - 文本底部的几个像素被截断

HTML/CSS : Image link partially covered by another image link is not clickable