【问题标题】:color transition on hover but not on focus/active?悬停时的颜色过渡但不是焦点/活动?
【发布时间】:2015-11-17 01:32:53
【问题描述】:

我希望我的按钮在悬停时有背景/字体颜色过渡,但在我单击它时没有(活动)。

目前我对悬停和活动都有过渡效果。 我尝试在 :active 或 :hover 上添加 transition: 0s ease-out;,但没有得到预期的结果。

什么是正确、最干净和最简单的方法(通过使用 css)?

button {
	position: absolute;
	width: 80px;
	height: 28px;
	font-size: 13px;
	font-weight: 700;
	line-height: 28px;
	color: #6cb4d5;
	font-family: 'Lato', sans-serif;
	padding: 0;
	border: 0;
	border-top-right-radius: 3px;
	border-bottom-right-radius: 3px;
	border-left: 1px solid #CCC;
	background-color: #fff;
	transition: 0.3s ease-out;
}

button:hover {
	color: #f7f7f7;
	background-color: #6cb4d5;
    outline: 0;
}

button:active {
	color: #f7f7f7;
	background-color: #398cb2;
	outline: 0;
}
<button type="submit" name="sub-avatar-url" id="sub-avatar-url"> UPLOAD</button>

非常感谢。

【问题讨论】:

    标签: css


    【解决方案1】:

    试试这个https://jsfiddle.net/2Lzo9vfc/93/

    button {
        position: absolute;
        width: 80px;
        height: 28px;
        font-size: 13px;
        font-weight: 700;
        line-height: 28px;
        color: #6cb4d5;
        font-family: 'Lato', sans-serif;
        padding: 0;
        border: 0;
        border-top-right-radius: 3px;
        border-bottom-right-radius: 3px;
        border-left: 1px solid #CCC;
        background-color: #fff;
        transition: all 0.3s ease-out;
    }
    
    button:hover {
        color: #f7f7f7;
        background-color: #6cb4d5;
        outline: 0;
    }
    
    button:active {
        color: red;
        background-color: blue;
        outline: 0;
        transition: none;
    }
    

    【讨论】:

    • 嗯,谢谢你的回答,但这不是我要找的。正如您在我的 sn-p 中看到的那样,当按钮处于活动状态时,我使用较暗的背景颜色,但我只是试图摆脱按钮悬停和单击(并处于活动状态)之间的过渡动​​画。 ;)
    • 哦,所以你想在悬停时过渡而不是在活动时
    • 更新了我的答案,请看一下。
    • 这是我得到的,但不知道如何放置:在原始按钮的位置之后 jsfiddle.net/2Lzo9vfc/97
    • 没问题,如果我能帮上忙,我很高兴。弄清楚这真的很有趣,而且比我最初认为的要复杂得多:)
    【解决方案2】:

    如果您希望按钮返回到 button:active 上的原始颜色,则将原始值添加到 :active 块将更正此问题。

    但是,如果您想在活动时将鼠标悬停在按钮上时显示颜色,只需删除 :active 块即可获得该效果。

    两者并排在这里。希望这是你想要的。 :)

    https://jsfiddle.net/2Lzo9vfc/94/

    button {
    	position: absolute;
    	width: 80px;
    	height: 28px;
    	font-size: 13px;
    	font-weight: 700;
    	line-height: 28px;
    	color: #6cb4d5;
    	font-family: 'Lato', sans-serif;
    	padding: 0;
    	border: 0;
    	border-top-right-radius: 3px;
    	border-bottom-right-radius: 3px;
    	border-left: 1px solid #CCC;
    	background-color: #fff;
            transition: all 0.3s ease-out;
    }
    
    button:hover {
    	color: #f7f7f7;
    	background-color: #6cb4d5;
            outline: 0;
    }
    
    
    #sub-avatar-url2:hover {
            color: #f7f7f7;
    	background-color: #6cb4d5;
            outline: 0;
    }
    
    #sub-avatar-url2:active {
            background-color: #fff;
            color: #6cb4d5;
            outline: 0;
            transition: none;
    }
    <button type="submit" name="sub-avatar-url" id="sub-avatar-url">&ensp;UPLOAD</button> <br><br>
    <button type="submit" name="sub-avatar-url2" id="sub-avatar-url2"> &ensp; UPLOAD</button>

    【讨论】:

    • 感谢您的回答,不幸的是,两者都不是:/ 我希望在按钮处于活动状态时显示更深的蓝色(如我的 css 所示)。你的第二个例子最接近我在过渡方面所能得到的(取消点击时仍然出现过渡效果)
    【解决方案3】:

    这可能对你有帮助-

    button {
        position: absolute;
        width: 80px;
        height: 28px;
        font-size: 13px;
        font-weight: 700;
        line-height: 28px;
        color: #6cb4d5;
        font-family: 'Lato', sans-serif;
        padding: 0;
        border: 0;
        border-top-right-radius: 3px;
        border-bottom-right-radius: 3px;
        border-left: 1px solid #CCC;
        background-color: #fff;
        transition: 0.3s ease-in;outline: 0;
    }
    
    button:hover {
        color: #f7f7f7;
        background-color: #6cb4d5;
        outline: 0;
    }
    
    button:active {
        color: #f7f7f7;
        background-color: #398cb2;
        outline: 0;transition:all 0ms ease-out
    }
    button:focus{transition:all 0ms ease-out}
    

    【讨论】:

    • 这实际上与我想要的非常接近,我看到的唯一问题是,当我取消悬停它时,单击它后按钮没有返回过渡。我将问题标记为已回答,但如果有人找到其他解决方案,我仍然很感兴趣。
    • button:focus{transition:all 300ms ease-out}
    • 相同的结果:/ - 我会深入研究
    【解决方案4】:

    您可以尝试将transition: 0s; 应用于活动语句 - 但是,您还需要将:focus 事件添加到声明链中,以防止释放单击时按钮转换。

    你可以在这里看到小提琴: http://jsfiddle.net/eks8Lc5c/

    【讨论】:

    • 感谢您的回答,但我不太喜欢使用 :focus 按钮,因为释放点击后按钮仍处于“按下状态”
    猜你喜欢
    • 1970-01-01
    • 2018-01-19
    • 2016-12-21
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2013-10-12
    • 2012-04-28
    • 2015-06-16
    相关资源
    最近更新 更多