【问题标题】:Disabling :hover during Css animation [closed]禁用:在CSS动画期间悬停[关闭]
【发布时间】:2015-08-04 17:07:55
【问题描述】:

所以我有一个侧边栏导航,当页面加载时,它会滑出屏幕,然后当用户将鼠标悬停在该区域上时,导航会滑回屏幕上。但是,当导航滑出时,如果用户在滑出动画期间将鼠标悬停在导航上,则导航会在尝试同时执行两种动画时开始闪烁。我想知道是否有办法防止这种情况和/或在滑出动画期间禁用 :hover?

侧边栏 asp.net

<div class="col-sm-3 col-md-2 sidebar slider"> 
     <h5 style="font-family: Helvetica; text-transform: uppercase">Releases</h5>
     <asp:CheckBoxList runat="server" ID="releaseStatusFilter" AutoPostBack="true" RepeatDirection="Horizontal" CellPadding="6" Height="5" style="margin-left:-10px">
     <asp:ListItem Text="Future" Value ="Future" Selected="true"></asp:ListItem>
     <asp:ListItem Text="Current" Value ="Current" Selected="true"></asp:ListItem>
     <asp:ListItem Text="Completed" Value ="Completed" Selected="false"></asp:ListItem></asp:CheckBoxList>
     <script type="text/javascript"></script>
     <asp:ListView runat="server" ID="releaseList" style="float:left;">
         <ItemTemplate>
             <ul class="nav navbar nav-sidebar goo-collapsible" >
                 <li><a href='<%# "Release.aspx?rn="+ Eval("releaseNumber") %>'><%# Eval("releaseNumber") + " " + Eval("releaseShortName") %></a></li>
             </ul>
             <script type="text/javascript">
                 var param = location.search.split('rn=')[1]
                 param = param.split('%20').join(' ')
                 $('ul').find('a[href="Release.aspx?rn=' + param + '"]').parents('li').addClass('active');
             </script>
        </ItemTemplate>
    </asp:ListView>
</div>

css

.sidebar {
    display: none;
}

@media (min-width: 768px) {
    .sidebar {
        font-size: 12px;
        position: fixed;
        top: 120px;
        width: 175px;
        bottom: 0;
        left: 0px;
        display: block;
        padding: 10px;
        overflow-x: hidden;
        overflow-y: auto; /* Scrollable contents if viewport is shorter than content. */
        background-color: #ccc;
        border-right: 1px solid #eeeeee;
        -webkit-animation: bannermoveout 0.5s linear both;
        animation: bannermoveout 0.5s linear both;
    }
}

@keyframes bannermoveout {
    0% {
        transform: translate3d(0px, 0px, 0px);
        animation-timing-function: ease-in;
    }

    50% {
        transform: translate3d(-92px, 0px, 0px);
        animation-timing-function: ease-in-out;
    }

    100% {
        transform: translate3d(-185px, 0px, 0px);
        animation-timing-function: ease-in-out;
    }
}

@-webkit-keyframes bannermoveout {
    0% {
        transform: translate3d(0px, 0px, 0px);
        animation-timing-function: ease-in;
    }

    50% {
        transform: translate3d(-92px, 0px, 0px);
        animation-timing-function: ease-in-out;
    }

    100% {
        transform: translate3d(-185px, 0px, 0px);
        animation-timing-function: ease-in-out;
    }
}

.sidebar:hover {
    -webkit-animation: bannermove 0.5s linear both;
    animation: bannermove 0.5s linear both;
}

@keyframes bannermove {
    0% {
        transform: translate3d(-185px, 0px, 0px);
        animation-timing-function: ease-in;
    }

    50% {
        transform: translate3d(-92px, 0px, 0px);
        animation-timing-function: ease-in-out;
    }

    100% {
        transform: translate3d(0px, 0px, 0px);
        animation-timing-function: ease-in-out;
    }
}

@-webkit-keyframes bannermove {
    0% {
        transform: translate3d(-185px, 0px, 0px);
        animation-timing-function: ease-in;
    }

    50% {
        transform: translate3d(-92px, 0px, 0px);
        animation-timing-function: ease-in-out;
    }

    100% {
        transform: translate3d(0px, 0px, 0px);
        animation-timing-function: ease-in-out;
    }
}

所以我设法通过使用 jquery 来为侧边栏菜单设置动画而不是 css 来修复它。谢谢大家的帮助!

jquery:

$(document).ready(function () {
            $(".slider").animate({
                left: '-185px'
            }, "fast")
            $("#slidebody").animate({
                left: '0px'
            }, "fast")
            .queue(function () {
                $(".slider").hover(function () {
                    $(".slider").stop().animate({ left: '0px' });
                    $("#slidebody").stop().animate({ left: "60px" });
                    $(this).dequeue();
                }, function() {
                    $(".slider").stop().animate({ left: '-185px' });
                    $("#slidebody").stop().animate({ left: "0px" });
                });
            });
        });

**slidebody 标签只是为了让我可以移动容器,这样导航就不会与容器重叠。 **

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

也许通过使用 .stop() 函数。看看这个:http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup

【讨论】:

  • 试过了,但没用,只是在左边的过渡处又增加了 20px。
  • .stop() 方法停止所有正在运行的 jQuery 动画。就是这样。在这种情况下它没有帮助。
  • @metalviper 这如何解决您的问题?
  • @IsmaelMiguel 他给我的链接帮助我设计了上面展示的 jquery 动画;这解决了我的问题。
  • @metalviper 对此无可争辩,但这个答案非常糟糕,而且容易导致链接腐烂。它甚至没有来自链接的任何内容。我认为这需要一个巨大的重写。但最终决定权在您。
【解决方案2】:

您总是可以尝试使用 jQuery 解除悬停事件的绑定。当你这样做时,你实际上是在告诉元素在你重新绑定它之前不再响应一个事件。这是一个非常普遍的例子:

//when the animation begins, unbind the hover event
$("sidebar").bind("animationstart", $(".sidebar").unbind("hover"));

//now, when the animation ends, rebind the hover event and call the function (in this case fxn())) that should fire on the hover event 
$("sidebar").bind("animationend", $(".sidebar").bind("hover", fxn));

这有帮助吗?

【讨论】:

  • 对不起,我对 js 和 jquery 还很陌生。当滑出过渡发生时我将如何解除悬停并在动画完成时重新绑定它?
  • 嗯,这取决于动画是如何开始的。我从未使用过 asp.net 侧边栏,但@Portekoi 回复中的链接看起来很有希望。我会更新我的答案来告诉你我的意思。
【解决方案3】:

尝试以下 CSS,重写以使用转换(我已删除 @mediaquery):

.sidebar {
	font-size: 12px;
	position: fixed;
	top: 20px;
	width: 175px;
	bottom: 0;
	left: 0px;
	display: block;
	padding: 10px;
	overflow-x: hidden;
	overflow-y: auto;
	background-color: #ccc;
	border-right: 1px solid #eeeeee;

	/*            v  magic starts there  v               */

	transition: ease-in-out 0.5s transform;
	-webkit-transition: ease-in-out 0.5s transform;
	transform: translate3d(-185px, 0px, 0px);
	-webkit-transform: translate3d(-185px, 0px, 0px);
}

.sidebar:hover {
	transform: translate3d(0px, 0px, 0px);
	-webkit-transform: translate3d(0px, 0px, 0px);
}
<div class="col-sm-3 col-md-2 sidebar slider"> 
    <h5 style="font-family: Helvetica; text-transform: uppercase">Releases</h5>
</div>

你在敲钉子!它不是为了被钉死,但它可以完成工作。但不如钉子。

为此设计了过渡,而不是动画。
动画旨在动画元素。
过渡意味着状态之间的平滑过渡

此外,这要短得多!


作为一种优化,我会改用边距。

如下:

.sidebar {
	font-size: 12px;
	position: fixed;
	top: 20px;
	width: 175px;
	bottom: 0;
	left: 0px;
	padding: 10px;
	overflow-x: hidden;
	overflow-y: auto;
	background-color: #ccc;
	border-right: 1px solid #eeeeee;

	/*            v  magic starts there  v               */
	
	transition: ease-in-out 0.5s margin-left;
	-webkit-transition: ease-in-out 0.5s margin-left;
	margin-left:-185px;
}


.sidebar:hover {
	margin-left: 0px;
}
<div class="col-sm-3 col-md-2 sidebar slider"> 
    <h5 style="font-family: Helvetica; text-transform: uppercase">Releases</h5>
</div>

【讨论】:

    猜你喜欢
    • 2013-10-22
    • 1970-01-01
    • 2015-02-19
    • 2020-10-14
    • 2021-04-02
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    相关资源
    最近更新 更多