简体   繁体   English

使用子标题缩小滚动条上的固定标题

[英]Shrinking fixed header on scroll with a sub header

I was looking for ways to resize (shrink) the header of my site on scroll. 我一直在寻找在滚动时调整(缩小)网站标题大小的方法。 Found an example which works good. 找到了一个很好的例子。 Working example here on JsFiddle JsFiddle上的工作示例

index 指数

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<meta name="description" content="" />
<title>Header Resize On Scroll with Animations</title>
<link href='http://fonts.googleapis.com/css?family=Ubuntu:300,400,700,400italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Oswald:400,300,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="css/style.css" />

<script src="js/classie.js"></script>

<script>
    function init() {
        window.addEventListener('scroll', function(e){
            var distanceY = window.pageYOffset || document.documentElement.scrollTop,
                shrinkOn = 300,
                header = document.querySelector("blk, header");
            if (distanceY > shrinkOn) {
                classie.add(header,"smaller");
            } else {
                if (classie.has(header,"smaller")) {
                    classie.remove(header,"smaller");
                }
            }
        });
    }
    window.onload = init();
</script>
</head>

<body>
<div id="wrapper">

<header>
    <div class="container clearfix">
        <div class="sub-header">
            hi
        </div>
        <h1 id="logo">
            LOGO
        </h1>
        <nav>
            <a href="">Lorem</a>
            <a href="">Ipsum</a>
            <a href="">Dolor</a>
        </nav>
    </div>
</header>
<div id="main">
    <div id="content">
        <section>
            <div class="container">
                <h1>Header Resize On Scroll with Animations</h1>
                <p>Cupcake ipsum dolor sit amet lollipop. Macaroon candy cotton candy bear claw macaroon carrot cake pastry icing dessert. Cupcake pastry tart sesame snaps lollipop donut pie. Cookie apple pie toffee lemon drops jelly beans cheesecake sweet roll. Jelly-o soufflé donut candy canes wafer dragée sweet cheesecake. Macaroon caramels pie cookie gummi bears. Ice cream jelly-o toffee cookie gingerbread cookie. Soufflé fruitcake jelly-o jelly chupa chups jelly beans. Dragée marzipan pastry macaroon oat cake muffin soufflé topping liquorice. Jelly-o chocolate cake lollipop.</p>
                <p>Sugar plum muffin cookie pastry oat cake icing candy canes chocolate. Gummi bears chupa chups fruitcake dessert jelly. Muffin cookie ice cream soufflé pastry lollipop gingerbread sweet. Unerdwear.com bonbon candy marzipan bonbon gummies chocolate cake gummi bears powder. Unerdwear.com tart halvah chocolate cake dragée liquorice. Sugar plum chocolate bar pastry liquorice dragée jelly powder. Jelly tootsie roll applicake caramels. Marzipan candy tootsie roll donut. Gummies ice cream macaroon applicake.</p>
                <p>
                    <a href="http://www.callmenick.com/tutorials/create-an-animated-resizing-header-on-scroll">&laquo; Go back to this tutorial?</a><br>
                    <a href="http://www.callmenick.com/tutorials">&laquo; Go back to all tutorials?</a>
                </p>
            </div>
        </section>
    </div>
</div>
</div>
</body>
</html>

css 的CSS

header {
width:100%;
height:150px;
overflow:hidden;
position:fixed;
top:0;
left:0;
z-index:999;
background-color:#0683c9;
-webkit-transition:height .3s;
-moz-transition:height .3s;
-ms-transition:height .3s;
-o-transition:height .3s;
transition:height .3s;
}

header h1#logo {
display:inline-block;
height:150px;
line-height:150px;
float:left;
font-family:Oswald, sans-serif;
font-size:60px;
color:#FFF;
font-weight:400;
-webkit-transition:all .3s;
-moz-transition:all .3s;
-ms-transition:all .3s;
-o-transition:all .3s;
transition:all .3s;
}

header nav {
display:inline-block;
float:right;
}

header nav a {
line-height:150px;
margin-left:20px;
color:#9fdbfc;
font-weight:700;
font-size:18px;
-webkit-transition:all .3s;
-moz-transition:all .3s;
-ms-transition:all .3s;
-o-transition:all .3s;
transition:all .3s;
}

header nav a:hover {
color:#FFF;
}

header.smaller {
height:75px;
}

header.smaller h1#logo {
width:150px;
height:75px;
line-height:75px;
font-size:30px;
}

header.smaller nav a {
line-height:75px;
}

.sub-header {
background-color:#000;
height:40px;
color:#999;
width:100%;
}

.clearfix:after {
visibility:hidden;
display:block;
content:"";
clear:both;
height:0;
}

and the js js

( function( window ) {

'use strict';

// class helper functions from bonzo https://github.com/ded/bonzo

function classReg( className ) {
  return new RegExp("(^|\\s+)" + className + "(\\s+|$)");
}

// classList support for class management
// altho to be fair, the api sucks because it won't accept multiple classes at once
var hasClass, addClass, removeClass;

if ( 'classList' in document.documentElement ) {
  hasClass = function( elem, c ) {
    return elem.classList.contains( c );
  };
  addClass = function( elem, c ) {
    elem.classList.add( c );
  };
  removeClass = function( elem, c ) {
    elem.classList.remove( c );
  };
}
else {
  hasClass = function( elem, c ) {
    return classReg( c ).test( elem.className );
  };
  addClass = function( elem, c ) {
    if ( !hasClass( elem, c ) ) {
      elem.className = elem.className + ' ' + c;
    }
  };
  removeClass = function( elem, c ) {
    elem.className = elem.className.replace( classReg( c ), ' ' );
  };
}

function toggleClass( elem, c ) {
  var fn = hasClass( elem, c ) ? removeClass : addClass;
  fn( elem, c );
}

var classie = {
  // full names
  hasClass: hasClass,
  addClass: addClass,
  removeClass: removeClass,
  toggleClass: toggleClass,
  // short names
  has: hasClass,
  add: addClass,
  remove: removeClass,
  toggle: toggleClass
};

// transport
if ( typeof define === 'function' && define.amd ) {
  // AMD
  define( classie );
} else {
  // browser global
  window.classie = classie;
}

})( window );

The problem. 问题。

What I have is a fixed Header, divided into 2 parts (header and sub header). 我有一个固定的标题,分为2部分(标题和子标题)。 Like the one below 像下面的那个

在此处输入图片说明

After adding the sub header to the code above the header shrinks as usual but the sub header (black in color) remains. 将子标题添加到上方的代码后,标题照常收缩,但子标题(黑色)仍然保留。 I want the header to shrink and the sub header to go away on scroll. 我希望标题缩小并且子标题在滚动时消失。

I tried a few ways over and over again but just could not get the sub header to go away on scroll 我一遍又一遍地尝试了几种方法,但是无法使子标题在滚动时消失

Can anyone of you please suggest how do I get this done. 谁能建议我如何做到这一点。

Consider adjusting your CSS (the smaller class definition), eg: 考虑调整CSS(较小的类定义),例如:

header.smaller {
    height: 5px; /* or even lesser */
}

Working example: https://jsfiddle.net/qc01zwyg/ 工作示例: https//jsfiddle.net/qc01zwyg/


Edit: 编辑:

For making the black sub go away, target its CSS under the .smaller selector. 为了使黑子消失,将其CSS定位在.smaller选择器下。

header.smaller {
  height: 75px;
}

header.smaller .menu {
  display: none;
}

Updated example: https://jsfiddle.net/qc01zwyg/2/ 更新的示例: https : //jsfiddle.net/qc01zwyg/2/

you should add style for sub header to change it 您应该为子标题添加样式以进行更改

.menu {
  background-color: #000;
  height: 40px;
  color: #999;
  width: 100%;
  transition: height .3s;
}

.smaller .menu {
  height: 0;
  overflow: hidden;
}

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

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