- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在滚动条上创建了一个粘性导航,其上方的标题会缩小。现在,当导航变得粘滞时,我试图让 Logo 从左侧进入。它有效,但来得太早了。如果你看一下我的演示,你会发现它出现在我不想要的标题上。有什么办法可以延迟它的进入,直到导航固定到顶部吗?
演示:https://codepen.io/Reece_Dev/pen/xqaEZX
$(document).on("scroll", function() {
if ($(document).scrollTop() > 20) {
$("header").addClass("shrink");
//setTimeout(function(){
$(".logo_animated").addClass("logo_display");
//}, 900);
} else {
$("header").removeClass("shrink");
$(".logo_animated").removeClass("logo_display");
}
});
* {
margin: 0;
padding: 0;
}
html {
width: 100%;
height: 100%;
}
body {
width: 100%;
height: 100%;
}
#head-background {
width: 100%;
background-color: #111;
position: fixed;
top: 0;
left: 0;
z-index: 1;
}
header {
width: 1200px;
height: 100px;
margin: 1em auto;
text-align: center;
overflow: hidden;
transition: all 0.8s ease-in-out;
-webkit-transition: all 0.8s ease-in-out;
-moz-transition: all 0.8s ease-in-out;
-o-transition: all 0.8s ease-in-out;
-ms-transition: all 0.8s ease-in-out;
}
.image {
display: inline-block;
margin: 0 auto;
max-width: 322px;
max-height: 100%;
padding-top: 5px;
}
.address-info {
float: left;
color: #fff;
width: 250px;
text-align: left;
font-size: 1rem;
padding-top: 5px;
padding-left: 1%;
background-color: aqua;
}
.head-icons {
float: right;
list-style-type: none;
display: block;
width: 250px;
text-align: right;
background-color: aqua;
}
.head-icons li {
display: inline-block;
padding-right: 3%;
}
.head-icons li a {
color: #fff;
font-size: 2.5rem;
}
.head-icons li:nth-child(4) {
display: block;
margin-top: 0.5rem;
color: #fff;
font-size: 1rem;
}
.shrink {
height: 0;
margin-top: 0;
margin-bottom: 0;
}
nav {
display: block;
width: 100%;
background-color: #777;
font-size: 0;
}
nav ul {
overflow: hidden;
color: #fff;
text-align: center;
-webkit-transition: max-height 0.4s;
-ms-transition: max-height 0.4s;
-moz-transition: max-height 0.4s;
-o-transition: max-height 0.4s;
transition: max-height 0.4s;
}
nav ul li {
display: inline-block;
padding: 1rem 2rem;
}
nav ul li a {
color: inherit;
text-decoration: none;
font-size: 1.5rem;
}
.fixed {
position: fixed;
top: 0;
left: 0;
right: 0;
}
.burger-button {
width: 100%;
text-align: right;
box-sizing: border-box;
padding: 0.5rem;
cursor: pointer;
color: #fff;
font-size: 1.5rem;
display: none;
}
.logo_animated {
position: absolute;
top: 1rem;
margin-left: -20rem;
width: 200px;
display: inline-block;
transition: margin-left 0.5s cubic-bezier(0.5, 0, 0.5, 1.6);
}
.logo_display {
margin-left: 1rem;
}
#body {
height: 2000px;
}
<div id="head-background">
<header>
<h4 class="address-info">00 The Street<br>Bramhope<br>Leeds<br>LS00 000</h4>
<img class="image" src="images/PopsiesLogoWhite.png">
<ul class="head-icons">
<li><a href=""><i class="fa fa-facebook-official" aria-hidden="true"></i></a></li>
<li><a href=""><i class="fa fa-twitter-square" aria-hidden="true"></i></a></li>
<li><a href="mailto:popsiesfishandchips@yahoo.co.uk"><i class="fa fa-envelope-o" aria-hidden="true"></i></a></li>
<li>0113 1234567</li>
</ul>
</header>
<nav>
<img class="logo_animated" src="images/popsies.svg">
<div class="burger-button">
<i class="fa fa-bars" aria-hidden="true"></i>
</div>
<ul id="height">
<li><a href="#welcomeAnchor">Welcome</a></li>
<li><a href="#menuAnchor">Menu</a></li>
<li><a href="#timesAnchor">Opening Times</a></li>
</ul>
</nav>
</div>
<div id="body"></div>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
最佳答案
您实际上可以做到这一点,没有任何延迟或添加/删除两个类。使用硬编码延迟实际上是一种不好的做法。如果你调整 CSS 的继承方式,你可以这样做:
Here's the updated codepen and relevant code below:
.shrink
添加到 JS 中的 #head-background
元素,并在 css 中应用规则,例如: #head-background.shrink header{
height: 0;
margin-top: 0;
margin-bottom: 0;
}
.logo_animated
类:#head-background.shrink .logo_animated {//apply this rule only when #head-background has .shrink class too.
margin-left: 1rem;
transition: margin-left .8s cubic-bezier(0.5, 0, 0.5, 1.6);
}
您的 JavaScript 现在看起来就像:
$(document).on("scroll", function() {
if ($(document).scrollTop() > 20) {
$("#head-background").addClass("shrink");
} else {
$("#head-background").removeClass("shrink");
}
});
请注意,您不需要单独添加/删除 .logo_display
类,它现在通过 css 继承来实现。
其工作原理如下:
您将收缩类应用于 head
和 .logo_animated
的父级。然后,您设置一个选择器 #head-background.shrink .logo_animated
,它告诉浏览器仅在 .shrink
类应用于父级之后应用规则。这基本上给了你想要的效果。请记住,您仍然需要正确调整动画时间。在 codepen 示例中,我使用 .8s
表示 .logo_animated
。您现在可以进一步细化您的动画,使其看起来更平滑,我基本上将其保留在您的位置
关于javascript - 延迟 addClass jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43046018/
如何使用 anchor 点击 addClass 和元素 addClass 一对一? 如何使用 jQuery add Class 和 removeClass 以及 JavaScript? one anc
我遇到了一个小问题。尝试更改样式/添加样式时,我不断收到以下错误: Object Gold has no method 'addClass' 这是我的 .js 文件中的代码: function Ge
有时我想在某些情况下在链中添加类。什么值在语义上最适合不添加类? 例子: $(".element").doSomething().addClass(condition ? "special-class
我有几个使用 Angular 执行动画的实例。 它们几乎相同 - 但一个有效,另一个无效 - 我无法确定第二个无效。 以下是有效的指令: - 它本质上为 element 中的一堆子元素设置动画。
我不确定 jQuery 类方法(addClass、removeClass 和toggleClass)是否充当标准 classList 方法的包装器,它们看起来与我非常相似: http://api.jq
我正在尝试编写这个脚本: 如果元素高度大于226,则addClass“portrait”,否则addClass“landscape” 我有这个: var assetHeight = $("video.
我正在尝试制作一个 slider 。我有四个内容,我把它们一个接一个地做成不可见的。然后我有一个名为 active-client 的类,它具有 display: flex 属性。我用 jQuery 的
请参阅下面的编辑。当我的验证 jquery/javascript 很差时,问题就出现了。从那以后,我实现了一个新的 css 类,它应该负责处理单个元素编辑之前所做的事情。同样,我的 jquery 验证
我正在使用这个 website 中的 JQuery eventcalendar 插件. 我的事件有开始日期和结束日期,我有以下代码在日历上添加一个绿色方 block (一个用于开始日期,一个用于结束日
我正在尝试使用 addClass 在 Joomla 模板上提供斑马条纹表格。我使用以下代码: jQuery(function($) { $("tr:odd").addClass("od
它的作用: $('p').addClass('bar'); 输出: 我需要什么: 那么是否可以将类添加到元素的第一个位置? 编辑(上下文):现在最后一个类有一个分割功能。如果添加另一个类,它也会
如何淡入淡出 .addClass。 这是链接 - http://www.bikramyogajoondalup.com.au/about-bikram-yoga/postures-benefits.h
我想创建这样的 HTML 元素 我使用javascript代码$('div').append(Array(3).fill($('test')).map((el, in
我有一组按钮,默认加载时没有我希望它们具有的类(来自第 3 方站点)。每次我单击按钮时,按钮都会发生变化 - 它们是用于滚动 的数字 (1 - 21)但它们并非全部同时显示。因此,在单击它们后,我一
我可以做 $(this).children("a").addClass("class") 并且它工作正常,但是当我做 $(this).addClass( “class”) 它不起作用。我在鼠标悬停函数
为什么 addClass 不起作用?单击带有 step 类的 div 时,div 背景和字体颜色应该发生变化,但没有任何反应。 .currentstep { bac
我有以下 html People 其中“a”的 CSS 为 color:#999999; font-size:31px; 我正在尝试使用 jQuery 来使用只有“color:#77
我有 Audi Select all 100 80 我尝试这样写: $(".select_all").click() { var models = $(this).parent().n
我有一个 style="display:none" 的 div。当鼠标悬停在链接上时,我想通过添加带有 display=block 的类来显示它,但它不起作用。 最佳答案 你需要在类中使用!impor
我使用这个CSS fa-info { background-color: green; font-size: 30px; } fa-info .active { background-co
我是一名优秀的程序员,十分优秀!