Animating two elements on scroll at specific point with speed problem

Trying to create an animation with two different images once a user reaches a specific point on the page. The animation works, but there is an issue when using a trackpad - the movement appears slow compared to scrolling with a mouse. I've already attempted setting the queue animation to false without success.

Any assistance would be greatly appreciated.

Here is my code:

<div class="logo-cnt">
    <div class="logo-glyph">
    <img src="https://placeholdit.imgix.net/~text?txtsize=20&txt=50%C3%9750&w=50&h=50">
    </div>
    <div class="logo-typeface">
        <img src="https://placeholdit.imgix.net/~text?txtsize=20&txt=120%C3%9750&w=120&h=50">
    </div>
</div>

<div class="lq">

</div>

jQuery :

var position = $('.lq').offset().top;
$(document).scroll(function () {
    var y = $(this).scrollTop();
    if(y > position){
        $(".logo-typeface").stop().animate({right:50, opacity:0}, 100, "linear");
        $(".logo-glyph").stop().animate({left:63}, 100);  
    }else{
        $(".logo-typeface").stop().animate({right:0, opacity:1}, 100, "linear");
        $(".logo-glyph").stop().animate({left:0}, 100);       
      }
});

View the live version here on JSFIDDLE

Answer №1

It appears that the issue at hand is the browser's interruption of Javascript execution during scrolling. While a mouse scrolls in noticeable increments (moving 10 y for each scroll position), a trackpad operates on a much finer scale.

To address this, I suggest utilizing CSS animations instead of Javascript, as they are not subject to interruptions.

http://jsfiddle.net/mkvz6uwu/1/

@keyframes logo-typeface-move-left {
    from {right: 0; opacity: 1;}
    to {right: 50px; opacity: 0;}
}

@keyframes logo-typeface-move-right {
    from {right: 50px; opacity: 0;}
    to {right: 0; opacity: 1;}
}

.logo-typeface-move-left {
    opacity: 0;
    right: 50px;
    animation-name: logo-typeface-move-left;
    animation-duration: .5s;
}

.logo-typeface-move-right {
    opacity: 1;
    right: 0;
    animation-name: logo-typeface-move-right;
    animation-duration: .5s;
}

@keyframes logo-glyph-move-right {
    from {left: 0;}
    to {left: 63px;}
}

@keyframes logo-glyph-move-left {
    from {left: 63px;}
    to {left: 0;}
}

.logo-glyph-move-right {
    left: 63px;
    animation-name: logo-glyph-move-right;
    animation-duration: .5s;
}

.logo-glyph-move-left {
    left: 0;
    animation-name: logo-glyph-move-left;
    animation-duration: .5s;
}

You can then use jQuery to add or remove classes based on the same criteria as before.

if (y > position) {
        $(".logo-typeface").removeClass('logo-typeface-move-right').addClass('logo-typeface-move-left');
        $(".logo-glyph").removeClass('logo-glyph-move-left').addClass('logo-glyph-move-right');
    } else {
        $(".logo-typeface").removeClass('logo-typeface-move-left').addClass('logo-typeface-move-right');
        $(".logo-glyph").removeClass('logo-glyph-move-right').addClass('logo-glyph-move-left');
    }

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

An odd issue has arisen where the website functions properly in Firefox 3.0 but encounters problems when accessed in Firefox 3

Could someone investigate this issue for me? When you click on the showcase and then on the logo, a modal window should open with the logo. It works perfectly in FF 3.0, but in FF 3.5, the tab switches from showcase to home after clicking the logo. Even ...

Selecting CSS tag excluding the first-child element

My goal is to use CSS to target the li elements in a list that do not have a description and are not the first-child with a b element. I want to apply padding-left to these specific li elements that do not have a title. <ul> <li><b>t ...

Utilizing the <style scoped> feature within my Angular template

When adding CSS styles in the specified htm file's templateUrl while loading a directive, I wonder if this is a bad idea. Will it be repeated every time the template is instantiated on the rendered page, or does Angular handle this differently? By usi ...

Enhance the flight experience of a helicopter with jQuery game by adding smoother controls

I'm currently experimenting with creating a basic game. In this game, the user controls a helicopter and can fly it up and down. Although I have successfully implemented the basic functionality where holding the screen makes the helicopter ascend an ...

focusing on a specific category within a CSS identifier

My goal is to specifically target the class .page-has-children within this code snippet: <div id="dc_jqverticalmegamenu_widget-2" class="widget sidebarwidget "> <div class="dcjq-vertical-mega-menu" id="dc_jqverticalmegamenu_widget-2-item"> ...

Is it possible for links to remain clickable even if they pass underneath a div

I have implemented a shadow using a div element to cover some content beneath it. However, I am facing an issue where the div that is under the shadow cannot be interacted with. Is there any solution to this problem? Thank you ...

CSS - Utilizing Flexbox for creating inline lists that adjust based on text length

I have a set of radio buttons arranged like this: I want to utilize flexbox to display them horizontally when there is sufficient space, similar to this: However, the current setup uses a min-width media query for layout breaking, which can lead to text ...

Allow users to edit the textarea only to input values, not from

I am trying to achieve a specific functionality with two input fields and one textarea. Whenever I type something in the input fields, their values are automatically populated in the textarea using .val(). Currently, this feature is working as intended, b ...

Develop an interactive AngularJS application with a dynamic Bootstrap table feature

I'm in the process of transitioning my existing jQuery code to AngularJS. One part of the code involves creating a dynamic Bootstrap table based on JSON data retrieved from a Spring REST service. The snippet below shows the jQuery code used to create ...

Discovering the Nearest Textfield Value Upon Checkbox Selection Using JQuery

How can I retrieve the value of the nearest Textfield after selecting a checkbox? This HTML snippet serves as a simple example. In my actual project, I have dynamically generated lists with multiple checkboxes. I require a way to associate each checkbox wi ...

Automate Div Updates with Jquery upon Insertion of New Records in MySQL Database

I'm in the process of creating a social networking website for my friends and I need help with updating a div that contains records from the database whenever a new record is added. This concept is similar to the live notifications on Facebook where t ...

Exploring the ancestors of an element

JS <script> $('.btn').click(function(e){ target = e.target; parent = target.parentNode.parentNode; console.log(parent); }); </script> HTML <div class="card" sty ...

Encountering a 403 Forbidden error while attempting to make an Ajax post request within the Django

Struggling to implement jQuery in a Django framework web application? Having difficulty getting a simple ajax call to function properly? Here is what my template file consists of, containing the form HTML and JavaScript to manage the ajax call: <script ...

UI experiencing issues with selecting radio buttons

When trying to select a radio button, I am facing an issue where they are not appearing on the UI. Although the buttons can be clicked, triggering a function on click, the selected option is not displayed visually. <div data-ng-repeat="flagInfo in avai ...

What is the best way to pinpoint the origin of the element.style being injected by JavaScript?

I've been tasked with updating a client's WordPress website, but I'm still getting acquainted with the overall structure of the site. When looking at the blog page (), I noticed that posts are displayed within a wrapper labeled #blogleft, a ...

Using CSS to showcase div elements with varying dimensions

How can I arrange divs with varying heights to be positioned close together, regardless of their height, and with a specific margin similar to buildings? I am aiming for a layout like the one on this website . I tried using float:left but it only float ...

Filtering a table using jQuery based on the class or data attributes

Issue arises when selecting the first "Icon" shows "Not found", then opting for "Talisman" does not display. It should show "Not Found". Is this achievable? Add the classes f-Icon, f-Ring, f-Neck. Then search for the value by class. Select either "Icon R ...

jQuery Mobile using the old value instead of the new one

Sending a variable through a link: <a href="foo.php?var=101"> php </a> <a href="foo.php?var=102"> html </a> <a href="foo.php?var=102"> css </a> Upon reaching foo.php: <!-- this part is under data-role="page" !--> ...

How can I remove the clear and "step" buttons from the Jquery Datepicker?

Recently, I took over a project from a client who no longer wants the "clear" buttons and the date increment "step" to be displayed. I have been struggling to remove these elements as they seem to be embedded within the input field itself, rather than bein ...

I need clarification on some basic concepts about ajax - Can you help?

Could someone please assist me in clarifying a concept? Currently, I am utilizing the colorbox plugin to load an external html snippet (which is functioning correctly). However, my jquery selectors are unable to detect the newly loaded html. Is this the co ...