Adjusting the height of a DIV element as the user scrolls

I have successfully minimized my navigation bar once the user scrolls past the header. However, I need advice on handling the line height of the elements as the div shrinks. Additionally, there is an issue where the navigation bar glitches and remains shrunken even when at the top of the page after erratic scrolling. Can anyone help me resolve this? Thank you. index.html

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Quench</title>

        <link rel="stylesheet" href="style.css" />

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="js/main.js" type="text/javascript"></script>
    </head>

    <body>

        <header id="site-header">
            <nav id="main-navigation">
                <ul class="soc" >
                    <li>Home</li>
                    <li>Specials</li>
                    <li>Location</li>
                    <li>Events</li>
                    <li class="social" style="text-align:center;">
                    <a class="soc-twitter" href="#"></a><a class="soc-facebook" href="#"></a><a class="soc-pinterest" href="#"></a>

                        </ul>
                    </li>
                </ul>
            </nav>


            <div class="logo">
            Quench
            <span>Satisfying Nightlife</span>
            </div>
            <div class="fore-glass">
            </div>
        </header>

        <section>
            <article>
                <header>
                <h2>Article Title</h2>
                </header>
                <p>Today massive amounts of penguins migrated to the warm waters of the Pacific Ocean</p>
            </article>

            <article>
                <header>
                <h2>Article Title 2</h2>
                </header>
                <p>German Sheperd kills man and small child walking in neighborhood park</p>
            </article>
        </section>

        <aside>
            <h2>About Section</h2>
            <p>Welcome to Quench the greatest site ever known to man</p>
        </aside>

        <div style="height:2000px"></div>

        <footer>
        Copyright 2015-2016 Quench.
        </footer>
    </body>

</html>

style.css

@import url(https://fonts.googleapis.com/css?family=Montserrat);

html, body {
margin:0;
padding:0;
background:#130806;
font-family: "Segoe UI", Candara, "Bitstream Vera Sans", "DejaVu Sans", "Bitstream Vera Sans", "Trebuchet MS", Verdana, "Verdana Ref", sans-serif;
color:#fff;
}

* {
    margin:0;
    padding:0;
}


img {
    max-width:100%;
}

footer {
background:#000;
color:#fff;
}

header#site-header {
background:url(images/bar-background.jpg);


background-size:100% 600px;
background-repeat:no-repeat;
background-attachment:fixed;
color:white;
width:100%;
height:600px;
position:relative;
overflow:hidden;

}

header#site-header .fore-glass {
    position:absolute;
    background:url(images/bar-glass.png);
    background-position: center top;
    background-size:25% 100%;
    background-repeat:no-repeat;
    right:6%;
    bottom:10%;
    width:50%;
    height:70%;
    z-index:6;


}

header#site-header .logo {
    position:absolute;
    top:50%;
    left:10%;
    color:#fff;
    font-size:90px;
    font-weight:900;
    width:500px;
    height:200px;
    text-align:center;
    margin-top:-100px;
    line-height:1;

}

header#site-header .logo span {
    font-size:80px;
    font-weight:500;
    font-style:italic;
    opacity:.7;
    color:#607927;
}

header#site-header nav#main-navigation {
    width:100%; 
    background:#607927;
    height:100px;
    font-size:24px;
    position:fixed;
    z-index:5;
    transition : all 100ms ease-in-out;

}
...

<article {
    padding:100px 0;
    max-width:640px;
    margin: 0 auto;
    text-align:center;


}

main.js

...

Here is the Fiddle link

http://jsfiddle.net/AFTef/1101/

TLDR Looking for advice on how to go about updating line-height of main-navigation as user scrolls past header.

Also, How can I fix the issue where if you use the scroll bar very fast, the header may be minimized when it should be full size?

Answer №1

Instead of manually creating CSS rules in JavaScript, it would be beneficial to define the two states in CSS and use JavaScript to toggle a class on and off based on the scroll position (excluding the transitional state between 500 and 550 pixels).

By avoiding dynamic setting of CSS transitions combined with other rules, you can prevent any randomness or unexpected behavior. CSS3 animations, while limited in scope, are often more efficient and easier to manage compared to JavaScript animations unless there is a specific task that CSS3 cannot handle.


For a simple and effective implementation, check out this example: http://jsfiddle.net/AFTef/1102/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

JavaScript string representing the actual value of a string

In order to reliably extract string literals from a JavaScript string, I need to create a function, let's name it f. Here are some examples: f('hello world') //-> 'hello world' (or "hello world") f('hello "world"') / ...

What is the best way to prevent the table from being added again once it has been displayed?

I'm faced with the task of displaying an HTML table in a popup window when a button is clicked, using Bootstrap modal functionality. This scenario is similar to a preview function where user input is displayed in a table when a preview button is click ...

Inserting multiple rows of data into a MySQL database in a single page using only one query in PHP

This snippet shows a MySQL query being used to update and insert data into a database: if ($_POST["ok"] == "OK") { $updateSQL = sprintf("UPDATE attend SET at_status=%s, at_remarks=%s WHERE at_tt_idx=%s", GetSQLValueString ...

absence of data in ajax response (or HTTP status code 206 Partial Content)

Feeling frustrated because I've wasted two hours trying to solve a simple task that I've done numerous times before. I'm not even sure where to start looking now. Struggling to retrieve static content using ajax from local servers (Apache a ...

Obtaining data from a Coldfusion CFC using a JQuery Ajax request

I am currently attempting to utilize Ajax in order to call a CFC component. However, I am struggling to pass the value that is returned by the CFC to my javascript function. The done function successfully creates the correct message which is displayed when ...

Azure-Graph is reporting an error: 'Invalid or missing Access Token.'

In my Node.js project, I effortlessly integrate azure APIs. Logging in: const MsRest = require('ms-rest-azure'); MsRest.loginWithServicePrincipalSecret(keys.appId, keys.pass, keys.tenantId); Creating a resource group: const { ResourceManageme ...

Issues with the orderBy function are causing unexpected behavior

After creating 3 groups - priority 1, 2, and 3 with orderBy:priorty for each pushed task to go into their designated group, I noticed some strange behavior within the groups where they don't sort properly when more data is added. <input ng-model=" ...

How to retrieve the outcome of a stored procedure using node.js

Is it possible to retrieve multiple select results from distinct tables (many rows) in just one stored procedure in mysql and then access those results in nodejs? In .NET with SQL Server, we can use "sqlnextresult" for this purpose. VIEW IMAGE FROM STORE ...

Modify the CSS code to transform the h1 heading into a unique and stylish

Can someone help me with replacing the following line with a logo that should be positioned after my fixed top menu? I have tried applying CSS code but nothing shows up. This is the CSS code I am using: #header .inner h1 a { float: left; display: ...

Background-color in CSS can be seen overlapping border on table cells in Internet Explorer

I am encountering an issue with a table containing two simple table cells: <table> <tr> <td>Test1</td> </tr> <tr> <td>Test2</td> </tr> </table> When I apply the following CSS to ...

I am having trouble retrieving the item from MongoDB using the React Native and Node.js combination

Within my index file, I have an API that retrieves a product from the database using a unique productId. After copying and pasting a specific productId from Mongo Atlas into Postman to test the API, it successfully retrieved the product as expected. Below ...

Leveraging AJAX with React Router

Currently tackling a project utilizing React Router, encountering some challenges with data flow. Each page triggers an AJAX call to fetch data for the component. These calls have been placed within componentDidMount: // The following code is in ES6 comp ...

The Nextjs Image was preloaded using link preload, but it remained unused after a short period following the window's load event

I've encountered an issue while working with Next.js, a React-based framework. I am attempting to display the logo.png image using the Image component provided by Next.js. My image is stored in this folder: public/img Here is the code I'm using ...

Retrieving Output from forEach Array Object

Is it possible to retrieve a value from a forEach array instance? const numArr = [10, 20, 30, 40, 50]; numArr.forEach(function (num, index, array) { console.log(array[index] + 100); }); While attempting to capture the console.log value, I found that it ...

How can we ensure only one click event is executed per element type in jQuery?

Here's the issue: I have a list of items with their own content organized in lists. Using jQuery, I've set it up so that when you click on an item, the list expands to show its content. However, when clicking on another item, the previously click ...

Efficiently merging data from various AngularJS resources

I've encountered a recurring scenario while developing Angular apps, especially when utilizing a Drupal API. I'm seeking advice on best practices for handling this specific issue. Currently, I am working on an app that interacts with a Drupal AP ...

Preventing browser auto-complete and hiding form sections with JQuery

Utilizing JQuery, I am able to hide a section of a form based on a radio button question with options "yes" (mapped to 1) and "no" (mapped to 0). When the user selects "yes," the hidden part of the form is revealed for them to fill out. Here is the code r ...

Flex items maintaining their size when the window decreases

My goal is to arrange two plotly plots side by side within a CSS flexbox, with the ability to resize them as the window size changes. The issue I'm facing is that while the plots expand correctly when the window is enlarged, they fail to shrink when t ...

What are the ways to utilize shorthand effectively?

My if blocks are getting too long. I'm looking for a way to use shorthand. Any suggestions? if (selected.value == "" || search.value == "") { data; } else if (selected.value == "Email") { data[& ...

Unable to retrieve information from the wiki API

Here is the link to my codepen project: https://codepen.io/mlestina/pen/OZpOQW?editors=1111 I am having trouble retrieving data from the Wiki API. When I check the Contr-Shift-J readout, I see: Loading failed for the <script> with source “https: ...