html/css - techniques for transitioning a centered image on a landing page into a navbar logo when scrolling

Creating a minimalist navbar without the use of JavaScript or custom CSS is easier than you think.

<nav class="navbar navbar-expand-md navbar-light bg-faded">
  <a class="navbar-brand" href="#">
    <img src="logo.jpg" alt="LOGO">
  </a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNav">
  </div>
</nav>
  

If you want to create a navbar with a logo image that transitions from center to left upon scrolling, it can be done seamlessly - even if you're not an animation expert.

To achieve this effect, consider these reference images for the initial and scrolled down states: Image for Not Scrolled down: https://i.sstatic.net/PkAli.png

Scrolled down Image: https://i.sstatic.net/LkPgx.png

Answer №1

1. Modifying the navbar by adding a second class called scroll and defining CSS styles for when it has the Scroll Class.

2. Implementing a transition effect in the default CSS.

3. Setting up an Event Listener on the window to retrieve the ScrollY value every time we scroll.

4. Including an if(condition) statement to check the scroll position. Essentially, if the ScrollY is less than 20, then add the class; otherwise, remove it.

let navBar = document.querySelector(".navbar");
window.addEventListener("scroll", (event) => {
  let scroll = this.scrollY;
  if (scroll < 20) {
    navBar.classList.add("scroll");
  } else {
    navBar.classList.remove("scroll");
  }
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-color: gainsboro;
  height: 200vh;
}

.navbar {
  position: fixed;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100vw;
  height: 60px;
  padding: 20px;
  background-color: white;
  box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.397);
  transition: all 0.5s ease-in-out;
}

.logo {
  height: 60px;
  transition: all 0.25s ease-in-out;
}

.navbar.scroll .logo {
  height: 500px;
}

.navbar.scroll {
  height: 100vh;
  width: 100vw;
  justify-content: center;
  overflow: hidden;
}

.navbar.scroll .navbar-toggler {
  display: none;
}
<body>
  <nav class="navbar scroll navbar-expand-md navbar-light bg-faded">
    <a class="navbar-brand" href="#">
      <img class="logo" src="https://d1csarkz8obe9u.cloudfront.net/posterpreviews/coffee-logo-design-template-938fdd0de81fdd844a5154e66b42a914_screen.jpg?ts=1625004947" alt="LOGO" />
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <svg class="navbar-toggler-icon" xmlns="http://www.w3.org/2000/svg"
                class="icon icon-tabler icon-tabler-menu-2" width="40" height="40" viewBox="0 0 24 24"
                stroke-width="1.5" stroke="#000000" fill="none" stroke-linecap="round" stroke-linejoin="round">
                <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                <line x1="4" y1="6" x2="20" y2="6" />
                <line x1="4" y1="12" x2="20" y2="12" />
                <line x1="4" y1="18" x2="20" y2="18" />
            </svg>
        </button>
    <!-- <div class="collapse navbar-collapse" id="navbarNav"></div> -->
  </nav>
  <script src="src/index.js"></script>
</body>

Answer №2

If you want to center a logo on your page, you can achieve this using the following code snippet. Additionally, for the top navigation bar, consider using the sample structure provided below:

<img src="example.jpg" alt="image" class="center">

<div class="topnav">
  <a class="active" href="/">Home</a>
  <a href="/about">About</a>
  <a href="/pages">Pages</a>
  <a href="/contact">Contact</a>
</div>

<style>
html, body {
  width: 100%;
  height: 100%;
}
.center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 20%;
}
.topnav {
  position: fixed;
  bottom: 0;
  width: 100%;
}
.active {
  background-color: #04AA6D;
  color: white;
}
</style>

Another approach is to utilize inline SVG elements which are easier to insert within the top navigation bar.

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

Expanding the number of tags in wp_nav_menu_container_allowedtags for WordPress

I'm currently working on designing a custom WordPress theme. By default, WordPress only allows the use of Div and Nav tags in wp_nav_menu_container_allowedtags. However, I am interested in adding an additional tag, namely Select. To achieve this, I ...

Troubleshooting: Issues with AngularJS $route.reload() functionality

I have an angular app and I'm attempting to refresh the page. I've tried using $route.reload() as recommended in multiple posts, but I can't seem to get it to work (Chrome is showing me an error). Here's my controller: var app = angula ...

JavaScript JSON conversion from a list

If I have two different lists, such as: list1= ['blue', 'green', 'purple'] list2 = ['circle', 'square', 'triangle'] Is there a way to convert them into a JSON object structure by using another l ...

Error message: "jQuery DataTables not working - table disappears when modal window opens

Currently working on an asp.net mvc application that utilizes the datatables grid. The grid is displayed and I have added a rowclick event to it: function CreateRowClickHandler() { var table = $('#table').dataTable(); $("#table > tbody").on(& ...

Incorporating a prop in keyframe animation with styled components

How should props be properly used within animations: ${styledKeyFrame} ${props.myProps}? The problem: import styled from 'styled-components'; const KeyFrameTest = styled.keyframes` from { opacity: 0; } to { opacity: 1; } `; const Style ...

Adjust the size of an input field in jquery or javascript based on user input

Ensure that an input text box can only contain a maximum of 13 numbers, but if the user enters a 14th number as a decimal point, then allow up to 16 numbers. HTML: <input type="text" maxlength="15" onkeyup="checkCurrency(this)"/> Script: func ...

Is it possible to nest a bootstrap "btn-toolbar" to create horizontal scrolling on smaller screens with Bootstrap 4?

I am utilizing the "btn-toolbar" class from bootstrap to contain multiple "btn-group" class dropdown buttons on a single line. For smaller screens, I want the btn-toolbar div to have a fixed width so that it can be horizontally scrollable. To achieve this, ...

form controls disappear upon adding form tags

Currently experiencing an issue with angular and forms that I could use some help with. I have a dynamic form that is functioning properly, but now I need to add validations to it. After following this guide, I established the structure correctly. Howeve ...

Convert ViewBag into a JSON array in order to create a bar chart in a .NET MVC application

I am struggling to convert ViewBag into a Json array in order to create a chart within the .Net MVC framework. Despite attempting to parse the Json data based on solutions from previous queries, the chart is not displaying anything. Here is my code: Contr ...

Arranging two spans to appear in a vertical column beside a thumbnail

My goal is to create a layout with a thumbnail, two pieces of information stacked underneath, and options floating to the right. I have attempted to achieve this using the following HTML and CSS: span.author_name { font-size: 14px; vertical-align: t ...

My script for retrieving the playlist is experiencing difficulties in calling upon the "btoa" and "item.slice" methods

Having trouble using the "btoa" method to encode the client id and client secret in base64, which are retrieved from the ".env" file. I've also attempted using the Buffer method but encountered an "invalid form" error with Buffer. Any assistance woul ...

What happens in mongoose if one of several consecutive queries fails?

Currently, as I work on my API, I am utilizing two Models: SongModel and UserModel. Whenever a new song is saved, I also execute a query to link the song._id to the user who created it. Unfortunately, a mistake in my code caused the second query to throw a ...

Utilizing MongoDB's object within the mapper function in MapReduce

I have a question about querying data in MongoDB using the aggregate framework. Originally, my data was structured like this: [ { created_at: "2014-03-31T22:30:48.000Z", id: 450762158586880000, _id: "5339ec9808eb125965f2eae1" } ] Now, ...

Exploring the Sidebar Navigation Features of Bootstrap 4

Looking to incorporate a vertical navigation menu on the left side of my webpage, extending the full height of the page. My attempt to achieve this using the Bootstrap grid system resulted in the navigation menu becoming too wide. This issue persisted eve ...

Maximizing Efficiency: Implementing Loading Panels in AngularJS

I need a loading icon (spinner) to appear when I press the button and the request is sent from the controller to the server. It may take some time for a response, so I want the loading icon to be displayed during this process. Once the server responds to t ...

In my attempt to position Font Awesome icons alongside a button icon and create equal spacing between them, I want to achieve the layout displayed in the example

How can I align Social Icons using Font Awesome like in the example provided below? I've tried targeting the Icons but I can't seem to adjust the padding or margin. What am I overlooking? - EXAMPLE <!--BOOTSTRAP--> <link rel="styles ...

How to retrieve properties of the final item in an array using Vue.js

Struggling with Vue.js JavaScript implementation. This is the current code: <div id="app"> <h1>Items</h1> <div v-for="item in items"> <input v-model="item.val"> </div> <button @click="addItem"> Ne ...

Verify if the scroll bar is still being held by the user

Is there a way to determine if the scrollbar is being grabbed by the user? Can the .scroll() method in jQuery be used for this purpose? $(window).scroll(function() { ... } }); I am looking to automatically scroll the div back to the top when the user ...

The error message "TypeError: Unable to access property of undefined when using web sockets"

Exploring Isomorphic framework for React and integrating Pusher for websockets communication. I'm encountering difficulty accessing the state within the componentDidMount() function. class TopbarNotification extends Component { state = { vis ...

JavaScript is unable to modify the value of an input in HTML

function modifyComment(id) { var pageID = "<?= $ID ?>"; var commentID = "p" + id; console.log(id); console.log(commentID); console.log(pageID); var commentContent = document.getElementById(commentID).innerHTML; < ...