javascript - 如何将过渡添加到 JavaScript 切换类

标签 javascript html css toggle transition

我在 JavaScript 中制作了一个切换类,其中有一个 div,它在鼠标悬停时切换其类。 div 的一个类具有彩色背景,另一个类具有图像。问题是图片是瞬间进来的,传输不流畅

我的 CSS 代码是:

.container{
}

.first{
    height: 100%;
    width: 33%;
    position: absolute;
    background-color: #ecf0f1;
    top: 0px;
    left: 0px;
    background-color: none;
    -webkit-transition: all 0.5s ease-in-out;
}
.sec{
    height: 100%;
    width: 33%;
    position: absolute;
    background-color: #ecf0f1;
    top: 0px;
    left: 0px;
    z-index: 5;
    background: url(1.jpg);
    background-color: none;
    -webkit-transition: all 0.5s ease-in-out;
} 

我的 HTML 代码是:

<div id="container" class="first"><span>HELOO</span>
</div>

最后我的 JavaScript 是:

this.classList.toggle('first');
        this.classList.toggle('sec');
        document.getElementById('#container').style.WebkitTransition = 'all 0.5s';
    }
    document.querySelector('#container').addEventListener('mouseenter', a )
    document.querySelector('#container').addEventListener('mouseout', a )
    document.getElementById('#container').style.WebkitTransition = 'all 0.5s';

最佳答案

你不能在背景图像上设置过渡效果,但有一些方法可以模拟它。看看这段代码:

jsFiddle

HTML

<div>
    <span>HELOO</span>
</div>

CSS

div{
    width:200px;
    height:200px;
    background:url(http://lorempixel.com/200/200);
    position:relative;
}
span{
    position:absolute;
    width:100%;
    height:100%;
    background-color:#ddd;
    -webkit-transition:all 0.5s ease;
    -moz-transition:all 0.5s ease;
    -ms-transition:all 0.5s ease;
    -o-transition:all 0.5s ease;
    transition:all 0.5s ease;
}
span:hover{
    background-color:transparent;
}

关于javascript - 如何将过渡添加到 JavaScript 切换类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20301574/

相关文章:

javascript - 如何在html表格中正确定义$(this)

javascript - 为什么我的网站在手机(ReactJS)上看起来完全不同?

c# - C# MVC4.0 中的有效 Math.Random

javascript - prototype.js - IE8 - 刷新后递归函数不起作用

javascript - == ""在 JavaScript 中的含义

html - Twitter Bootstrap 中的响应式表格处理

javascript - 使用下拉菜单更改图像

javascript - 删除同步图表中的工具提示,当用户离开图表区域时

jquery - Firefox(通过 jQuery)报告没有 src 属性的图像高度错误

css - 如何删除 JavaFX 8 TableView 中的条纹行