CSS Transition hover effect for touch devices

吃可爱长大的小学妹 提交于 2020-01-07 06:58:15

问题


I'm trying to get a hover/ touch effect when I tap a button using CSS. I have used the below CSS transition to change the background, but it doesn't seem to work:

https://jsfiddle.net/s3vbgos7/

HTML

<div role="button" class="ico-btn red"></div>

CSS

.ico-btn {
    -webkit-user-select: none; margin-right: 0px;
    -webkit-box-shadow: rgba(0, 0, 0, 0.298039) 0px 2px 10px 0px, rgba(0, 0, 0, 0.0980392) 0px 0px 1px 0px, rgba(255, 255, 255, 0.247059) 0px 1px 0px 0px inset, rgba(0, 0, 0, 0.14902) 0px -1px 0px 0px inset;
    -webkit-font-smoothing: antialiased;
    border-bottom-left-radius: 25%;
    border-bottom-right-radius: 25%;
    border-top-left-radius: 25%;
    border-top-right-radius: 25%;
    box-shadow: rgba(0, 0, 0, 0.298039) 0px 2px 10px 0px, rgba(0, 0, 0, 0.0980392) 0px 0px 1px 0px, rgba(255, 255, 255, 0.247059) 0px 1px 0px 0px inset, rgba(0, 0, 0, 0.14902) 0px -1px 0px 0px inset;
    cursor: pointer;
    display: block;
    font-family: Roboto, Arial, sans-serif;
    font-size: 140px;
    font-weight: normal;
    height: 177px; /*167px;*/
    line-height: 150px;
    min-width: 0px;
    padding: 40px 0 0 0; /*30*/
    text-align: center;
    white-space: nowrap;
    width: 220px;/*200*/
    z-index: 2;
    margin: 0 auto;
    color:#fff;
}

.ico-btn.red { background-color: rgb(219, 68, 55);
    background-image: -moz-linear-gradient( 90deg, rgb(170,34,23) 0%, rgb(170,34,23) 0%, rgb(195,51,39) 22%, rgb(219,68,55) 100%);
    background-image: -webkit-linear-gradient( 90deg, rgb(170,34,23) 0%, rgb(170,34,23) 0%, rgb(195,51,39) 22%, rgb(219,68,55) 100%);
    background-image: -ms-linear-gradient( 90deg, rgb(170,34,23) 0%, rgb(170,34,23) 0%, rgb(195,51,39) 22%, rgb(219,68,55) 100%);
    border-bottom-color:#ab2318;
    border-bottom-left-radius: 25%;
    border-bottom-right-radius: 25%;
    border-bottom-style: solid;
    border-bottom-width: 1px;
    border-left-color: rgb(0, 0, 0);
    border-left-style: none;
    border-left-width: 0px;
    border-right-color: rgb(0, 0, 0);
    border-right-style: none;
    border-right-width: 0px;
    border-top-color:#ef675c;
    border-top-left-radius: 25%;
    border-top-right-radius: 25%;
    border-top-style: solid;
    border-top-width: 2px;
    transition: all .1s linear;
}
.ico-btn.red:hover {
    background-color: #D05944;
    -webkit-transition: background-color .25s ease-in-out;
    -moz-transition: background-color .25s ease-in-out;
    -o-transition: background-color .25s ease-in-out;
    transition: background-color .25s ease-in-out;
}

回答1:


Your background-image: -moz-linear-gradient( 90deg, rgb(170,34,23) 0%, rgb(170,34,23) 0%, rgb(195,51,39) 22%, rgb(219,68,55) 100%); is overriding the background-color in the :hover state.

If you loose the background-image, it will work:

.ico-btn {
    -webkit-user-select: none; margin-right: 0px;
    -webkit-box-shadow: rgba(0, 0, 0, 0.298039) 0px 2px 10px 0px, rgba(0, 0, 0, 0.0980392) 0px 0px 1px 0px, rgba(255, 255, 255, 0.247059) 0px 1px 0px 0px inset, rgba(0, 0, 0, 0.14902) 0px -1px 0px 0px inset;
    -webkit-font-smoothing: antialiased;
    border-bottom-left-radius: 25%;
    border-bottom-right-radius: 25%;
    border-top-left-radius: 25%;
    border-top-right-radius: 25%;
    box-shadow: rgba(0, 0, 0, 0.298039) 0px 2px 10px 0px, rgba(0, 0, 0, 0.0980392) 0px 0px 1px 0px, rgba(255, 255, 255, 0.247059) 0px 1px 0px 0px inset, rgba(0, 0, 0, 0.14902) 0px -1px 0px 0px inset;
    cursor: pointer;
    display: block;
    font-family: Roboto, Arial, sans-serif;
    font-size: 140px;
    font-weight: normal;
    height: 177px; /*167px;*/
    line-height: 150px;
    min-width: 0px;
    padding: 40px 0 0 0; /*30*/
    text-align: center;
    white-space: nowrap;
    width: 220px;/*200*/
    z-index: 2;
    margin: 0 auto;
    color:#fff;
}

.ico-btn.red { background-color: rgb(219, 68, 55);
    border-bottom-color:#ab2318;
    border-bottom-left-radius: 25%;
    border-bottom-right-radius: 25%;
    border-bottom-style: solid;
    border-bottom-width: 1px;
    border-left-color: rgb(0, 0, 0);
    border-left-style: none;
    border-left-width: 0px;
    border-right-color: rgb(0, 0, 0);
    border-right-style: none;
    border-right-width: 0px;
    border-top-color:#ef675c;
    border-top-left-radius: 25%;
    border-top-right-radius: 25%;
    border-top-style: solid;
    border-top-width: 2px;
    transition: all .1s linear;
}
.ico-btn.red:hover {
	background-color: #D05944;
	-webkit-transition: background-color .25s ease-in-out;
	-moz-transition: background-color .25s ease-in-out;
	-o-transition: background-color .25s ease-in-out;
	transition: background-color .25s ease-in-out;
}
<div role="button" class="ico-btn red"><span class="icon-sad"></span>
</div>



回答2:


In ico-btn.red remove following lines as they override hover state:

background-color: rgb(219, 68, 55);
    background-image: -moz-linear-gradient( 90deg, rgb(170,34,23) 0%, rgb(170,34,23) 0%, rgb(195,51,39) 22%, rgb(219,68,55) 100%);
    background-image: -webkit-linear-gradient( 90deg, rgb(170,34,23) 0%, rgb(170,34,23) 0%, rgb(195,51,39) 22%, rgb(219,68,55) 100%);
    background-image: -ms-linear-gradient( 90deg, rgb(170,34,23) 0%, rgb(170,34,23) 0%, rgb(195,51,39) 22%, rgb(219,68,55) 100%);


来源:https://stackoverflow.com/questions/32228098/css-transition-hover-effect-for-touch-devices

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!