html - 在页面加载时显示滑动 div

标签 html css

我有一个 div 固定在我的页面左侧,它开始部分隐藏在页面之外,然后在鼠标悬停时平滑地滑入。我想要做的是让 div 在页面加载时完全可见,然后在几秒钟后滑开,然后具有其正常的鼠标悬停功能。

有问题的 div:

HTML

<a href="#" target="_blank">
    <div id="calculator">Novated Lease Calculator</div>
</a>

CSS

#calculator {
    width: 297px;
    height: 60px;
    margin: 0;
    padding: 0;
    text-indent: -9999px;
    background: url("../img/calculator-button.png") no-repeat;
    position: fixed;
    top: 218px;
    left: -247px;
    z-index: 999;
    -webkit-transition-duration: 0.3s;
    -moz-transition-duration: 0.3s;
    -o-transition-duration: 0.3s;
    transition-duration: 0.3s;
}

#calculator:hover {
    left: 0;
}

如果可能,我想使用 CSS 解决方案;但是,如果别无选择,javascript 也可以。

最佳答案

您可以通过纯 css 来做到这一点,尽管有点 hack。试试这个

CSS

#calculator {
    width: 297px;
    height: 60px;
    margin: 0;
    padding: 0;
    text-indent: -9999px;
    background: #f00;
    position: fixed;
    top: 218px;
    left: -247px;
    z-index: 999;
    -webkit-transition-duration: 0.3s;
    -moz-transition-duration: 0.3s;
    -o-transition-duration: 0.3s;
    transition-duration: 0.3s;
    animation: slideOnLoad 1s 1;
    -webkit-animation: slideOnLoad 1s 1;
}

@keyframes slideOnLoad
{
    0% {left:-247px;}
    0.1% {left:0px;}
    100% {left:-247px;}
}
@-webkit-keyframes slideOnLoad
{
    0% {left:-247px;}
    0.1% {left:0px;}
    100% {left:-247px;}
}

#calculator:hover {
    left: 0;
}

工作示例是 here

解释我做了什么。我已经使用动画 css3 属性在加载时为您的元素设置一次动画。 0-0.1% 的解决方案在这里有点变通。这使您的元素保持在正确的位置,然后像风一样快速将其移动到 left:0,并开始平滑的动画以再次隐藏该元素。剩下的就是您的悬停解决方案。

您可以随心所欲地玩计时。如果你想让你的元素停留更长的时间——只需添加 20% 的动画步长 {left:0px;}

请注意,css3 animate 不适用于 IE9 等旧浏览器。检查浏览器列表 here

关于html - 在页面加载时显示滑动 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602270/

相关文章:

html - 无法让 div 滚动 [css]

html - 如何用css在手机上实现按钮点击效果

asp.net - 清除属性未按预期工作

php - 如何使用 php 从多行表单更新 mysql 数据库

javascript - AngularJs 在验证后将用户数据保存到 session 中

html - 了解浏览器呈现 "quirk"

javascript - 在标签标签上使用 this.form.submit()

php - css链接每天都会自动改变

html - Angular - 使用 NgStyle 动态更改 CSS 属性

html - Bootstrap 3 中的奇怪按钮包装 - 响应式设计