简体   繁体   English

scrolltop上的javascript动画div高度+背景颜色

[英]javascript on scrolltop animate div height + background color

I'm building my own photography website with a header that changes when the user scrolls down the page.我正在构建自己的摄影网站,其标题在用户向下滚动页面时会发生变化。 The HTML menu/header looks like this: HTML 菜单/标题如下所示:

<div id="menu" class="fluid">
header/menu content
</div>

Using CSS I created a transparent black background and gave the menu a height:我使用 CSS 创建了一个透明的黑色背景,并为菜单设置了高度:

#menu {
height: 100px;
background-color: rgba(0,0,0,0.1);
}

Now, using JavaScript, I made this header collapse to 75 pixels when the user scrolls down more than 5 pixels.现在,使用 JavaScript,当用户向下滚动超过 5 个像素时,我将此标题折叠到 75 个像素。 When you scroll back to the top it goes back to 100px.当您滚动回顶部时,它会回到 100 像素。 The JavaScript code looks like this: JavaScript 代码如下所示:

<script>
// Minimize menu on scroll
  $(window).on('scroll', function () {
  var scrollTop = $(window).scrollTop();
  if (scrollTop > 5) {
      $('#menu').stop().animate({height: "75px"},150);
  }
  else {
      $('#menu').stop().animate({height: "100px"},150);
  }
  });
</script>

I am looking for an easy way to change the background transparency together with the height of the menu and was hoping anyone knew what code to use for it.我正在寻找一种简单的方法来更改背景透明度和菜单的高度,并希望有人知道要使用什么代码。 I have no experience with JavaScript, only with HTML and CSS code.我没有 JavaScript 经验,只有 HTML 和 CSS 代码。

Thanks in advance, and I'm sorry if this question was asked before (I couldn't find it using my keywords).提前致谢,如果之前有人问过这个问题,我很抱歉(我无法使用我的关键字找到它)。

~ Elmigo ~ 埃尔米戈

You can make annimation just using css transition : by example above I've just added transition property on opacity then I managed to change it on scrolling.您可以仅使用 css transition 制作动画:通过上面的示例,我刚刚在 opacity 上添加了 transition 属性,然后我设法在滚动时对其进行了更改。

not that I set and rgb color value instead of rgba , also set opacity to 0 .1;不是我设置和 rgb 颜色值而不是 rgba ,也将不透明度设置为 0 .1;

 // Minimize menu on scroll $(window).on('scroll', function () { var scrollTop = $(window).scrollTop(); if (scrollTop > 5) { $('#menu').stop().animate({height: "50px"},150); //$('#menu').css("opacity","0.8"); $('div').css('background-color', 'rgba(0,0,0,0.8)') } else { $('#menu').stop().animate({height: "100px"},150); //$('#menu').css("opacity","0.1"); $('div').css('background-color', 'rgba(0,0,0,0.1)') } });
 #menu { height: 100px; background-color: rgba(0,0,0,0.1); transition:background-color 0.5s ease; color:green; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="menu" class="fluid"> <p>gggg<p> </div> text <br>text <br>text <br>text <br>text <br>text <br>text <br>text <br>text <br>text <br>text <br>text <br>text <br>text <br>text <br>text <br>text<br>text<br>text

Make a class做一堂课

.darkerbg {
  background-color: rgba(0,0,0,0.5);
}

Add/remove it at the appropriate times在适当的时候添加/删除它

if (scrollTop > 5) {
  $('#menu').stop().animate({height: "75px"},150);
  $('#menu').addClass('darkerbg');
}
else {
  $('#menu').stop().animate({height: "100px"},150);
  $('#menu').removeClass('darkerbg');
}

Additionally, you could move all this into CSS by changing the height with the class like so:此外,您可以通过更改类的高度将所有这些移动到 CSS 中,如下所示:

.menu {
  //various properties
  transform: all 0.1s; // this provides the animation effect
}
.big {
  height: 100px;
  background-color: rgba(0,0,0,0.1);
}
.small {
  height: 75px;
  background-color: rgba(0,0,0,0.5);
}

Then do this with the JS:然后使用 JS 执行此操作:

if (scrollTop > 5 && !$('#menu).hasClass('small')) {
  $('#menu').removeClass('big'); //just incase it is there
  $('#menu').addClass('small');
} else if (scrollTop <= 5 && !$('#menu).hasClass('big')) {
  $('#menu').removeClass('small'); //just incase it is there
  $('#menu').addClass('big');
}

I would say amflare's answer is almost correct.我会说 amflare 的回答几乎是正确的。 I'd remove the animate from the javascript altogether and do it this way:我会完全从 javascript 中删除animate并这样做:

CSS: CSS:

.darkerbg {
  height: 75px;
  background-color: rgba(0,0,0,0.5);
}

#menu {
height: 100px;
background-color: rgba(0,0,0,0.1);
transition: all 0.5s;
}

JAVASCRIPT:爪哇脚本:

if (scrollTop > 5) {
  $('#menu').addClass('darkerbg');
}
else {
  $('#menu').removeClass('darkerbg');
}

If you want to animate the changing of the background-color , you can use jquery-ui plugin: jQuery UI .如果你想动画background-color的变化,你可以使用jquery-ui插件: jQuery UI I find it very useful and it's workink fine and pretty.我发现它非常有用,而且工作墨水很好很漂亮。

For example:例如:

First, include the jQuery UI in your's project:首先,在您的项目中包含jQuery UI

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="></script>

And change the Javascript content to:并将Javascript内容更改为:

<script>
// Minimize menu on scroll
$(window).on('scroll', function () {
    var scrollTop = $(window).scrollTop();
    if (scrollTop > 5) {
        $('#menu').stop().animate({height: "75px", backgroundColor: "rgba(0,0,0,0.5)"}, 150);
    }
    else {
        $('#menu').stop().animate({height: "100px", backgroundColor: "rgba(0,0,0,0.1)"},150);
    }
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM