Revealing CSS elements moving from the left to the right

On my website, I have implemented two different menu bars. The first one, in black, stays fixed at the top of the page. The second one, in orange, becomes sticky once it reaches the black bar. At the same time, a small logo image is revealed, previously hidden using the "inactive" class with "display: none". It then appears by removing the "inactive" class and adding the "active" class with "display: inline-block".

View the animated gif here:

Check out the live version here:

However, the transition from "display: none" to "display: inline-block" is quite sudden. I need assistance in creating a smoother transition where the logo is revealed from left to right, gently pushing the social icons to the right.

Below is the code snippet:

HTML

<div class="topbar-container">
    <div id="slc" class="notonmobile scroll-logo inactive">
        <a href="http://mydivision.net/" title="MYDIVISION.NET | Home">
            <img src="http://mydivision.net/wp-content/themes/v1/img/scroll-logo-small.png" alt="Logo" />
        </a>
    </div>
    <div id="social_container">
        ...
    </div>
    ...
</div>
</div>

CSS

#topbar {display: inline-block; width: 100%; padding: 0; height: 40px; background: #000; position: fixed; top: 0; z-index: 9999;}
.topbar-container {margin: 6px 20px; line-height: 24px;}
.scroll-logo.inactive {display: none;}
.scroll-logo.active {display: inline-block; float: left; margin-right: 20px;}
.scroll-logo img {height: 24px; padding: 3px 0;}
#social_container {float: left; width: 192px;}

jQuery

jQuery(document).ready(function($) {
    $(window).scroll(function() {
        if ($(this).scrollTop() >= $('.sticky-element-active').offset().top - 40) {
            $("#slc").addClass("active");
            $("#slc").removeClass("inactive");
        } else {
            $("#slc").removeClass("active");
            $("#slc").addClass("inactive");
        }
    });
});

Answer №1

After making some adjustments to your live CSS, I was fortunate enough to see some positive results. Feel free to experiment with the transition times and make further tweaks if needed. Hopefully, this will guide you in the right direction.

.modified-logo.inactive {
  transition: 0.7s;
  transform: translateX(-80%);
  opacity: 0.2;
  height: 10%;
}

.modified-logo.active {
  transition: 0.5s;
  transform: translateX(20px);
  opacity: 0.9;
  float: left;
  margin-right: 30px;
}

https://i.stack.imgur.com/5W1UO.gif

Answer №2

Replace the usage of active and inactive classes in your CSS with the code provided below. Using Jquery's show() and hide() functions will cause elements to expand and contract when a duration is set for them.

However, ensure that you still have the inactive class specified in your HTML so that the expansion only occurs once each time the scroll condition is met:

jQuery(document).ready(function($) {
    $(window).scroll(function() {
        if ($(this).scrollTop() >= $('.sticky-element-active').offset().top - 40 && $("#slc").hasClass("inactive")) {
            $("#slc").show(1000);
            $("#slc").removeClass("inactive");
        } else {
            $("#slc").hide(1000);
            $("#slc").addClass("inactive");
        }
    });
});

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

Tap here to switch between 2 jquery functions

Due to the deprecation of the toggle() method in jQuery version 1.8 and its removal in version 1.9, an alternative approach is needed for versions 1.11 and above. You can check out the documentation for more information. If you are looking to replicate th ...

Constant updates similar to FourSquare

I'm looking to add a real-time status update feature inspired by popular websites using ASP.NET MVC + jQuery Is there anyone aware of tutorials or samples that could help me achieve this? ...

Minimize the entire project by compressing the .css, .js, and .html files

After recently incorporating Grunt into my workflow, I was thrilled with how it streamlined the process of minifying/concatenating .css files and minifying/uglify/concatenating .js files. With Grunt watch and express, I was able to automate compiling and ...

What causes the table to expand with new cells once the database information is shown?

I'm confused as to why an empty cell is being added to the right even if there is data from the database to display. This particular code is meant to showcase a school schedule. Attached is a picture showing how it currently appears. Below is the PH ...

What is the best way to determine the length of an array using input from an HTML form?

Currently, I am in the process of developing a PHP file and one feature that I would like to implement is creating an array as illustrated in Example 1. As far as my understanding goes, an Array functions like a list where items can be added as shown in Ex ...

Discovering the true width of an element using JavaScript

Hey there, I'm currently attempting to determine the exact width of a specific element and display an alert that shows this width. The code I am using is as follows: HTML Element <table cellspacing="1" cellpadding="5" style=";width:975px;border: ...

Having trouble getting my ReactJS page to load properly

I am currently linked to my server using the command npm install -g http-server in my terminal, and everything seems to be working smoothly. I just want to confirm if my h1 tag is functional so that I can proceed with creating a practice website. I have a ...

To make a JavaFX label stand out, simply prepend a vibrant red asterisk at the start of

I'm facing a challenge with my SceneBuilder project. I have developed a scene that consists of several labels, and I want to highlight some of them by adding a red asterisk at the beginning of their text. Unfortunately, I haven't been able to fin ...

Switching from HTML to BBCode during the editing process

My issue lies in converting BBCode to HTML and then editing the code on a page with AJAX. When editing in a <textarea>, the HTML tags show up instead of the original BBCode. For instance, if I submit [b]bold text[/b] and save it to my database, the ...

Tips for aligning a custom icon to the left in a Jquery mobile header

I have successfully implemented a custom icon in the header, but it is currently displaying in the center. How can I adjust it to be positioned on the left side instead? <div data-role="header" data-theme="a"> <h3> ...

Issues encountered when trying to use ng-repeat alongside a multiselect plugin

<select class="form_control" id="district" name="district" multiple ng-model="$scope.district"> <option value="{{tp_id}}" ng-repeat="tp_id in district" >{{tp_id}} </option> </select> I am facing an issue where no v ...

Is there a way to transfer table row data to another table by simply clicking on the corresponding checkbox in the same row?

I'm working with a table that includes 4 fields: service, amount, tax, and action. My challenge is to have the data from any row in the first table added to a second table when its checkbox is selected. The second table should have the same fields a ...

Element in Vue not displaying when modal is hidden

Having trouble displaying a Vue component on a modal controlled by semantic ui. The Vue element is not loading or mounting properly. My setup involves Laravel and jQuery with some Vue elements intertwined. I have a hunch that the issue stems from the moda ...

Leverage scope variables and dynamic CSS properties when using ngClass

Is there a way to manipulate the CSS dynamically in HTML using a scope variable? <div ng-class="myClassScope, { 'dynamic-class': !ifIsNot }"> ...

Submitting quiz responses through a PHP based form

I am currently facing an issue with integrating forms and a quiz on my website. Individually, both work fine but when combined, they do not function as expected. Specifically, the quiz itself operates correctly, however, it fails to send the results via P ...

Footer overlapping occurs on a single page

My webpage is having an issue where the footer is overlapping. It seems to be fine on all other pages except for this one. I would prefer not to make any changes to the footer since it's working correctly on other pages. Is there a way to adjust the C ...

Transfer information from .gs (Google Apps Script) to a variable inside a <script> tag in an HTML document

[ {"id":1,"label":"Node 2"}, {"id":2,"label":"Node 3"}, {"id":3,"label":"Node 4"}, {"id":4,"label":"Node 5"} ] Hello there! The function getArray() in the code snippet above is returning the specified string ↑. I need help connecting this data w ...

The Google Share button may fail to be displayed correctly if it was initially hidden

Is there a solution to the issue where the Google Share button does not display properly when it is initially hidden using CSS display:none on its parent div and then shown using jQuery .show()? I am currently experiencing this problem and I'm unsure ...

Getting a null value for active user after completing the sign-in process

I am using local storage to store username and password. However, I am encountering an issue where the active user is returning null after a certain line of code, and I am unsure why. console.log("I am the Active user: " + activeUser); const me ...

Relocate the resizable handles in jQuery outside of the div elements

I currently have 3 nested divs. Using jQuery $(function() { $("#div1").resizable({ handles: "n, e, s, w, nw, ne, sw,se" }); $("#div1").draggable(); }); Within the HTML structure <div id="div1" style="left: ...