Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7410243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:12:21+00:00 2026-05-29T06:12:21+00:00

In an answer to another question I made a simple bit of code to

  • 0

In an answer to another question I made a simple bit of code to animate the rotation of a PNG as you scrolled down the page. That worked fine but on reversing the animation on the up-scroll didn’t work properly.

Expected would be that the PNG would go back to it’s original position but it falls short by a couple of pixels .

fiddle : http://jsfiddle.net/EnSkJ/3/

code :

var sdegree = 0;


var lastScrollTop = 0;
$(window).scroll(function(event){
   var st = $(this).scrollTop();
   if (st > lastScrollTop){
       sdegree += 1;
    var srotate = "rotate(" + sdegree + "deg)";
    $("img").css({"-moz-transform" : srotate, "-webkit-transform" : srotate});
   } else {
      sdegree -= 1;
    var srotate = "rotate(" + sdegree + "deg)";
    $("img").css({"-moz-transform" : srotate, "-webkit-transform" : srotate});
   }
   lastScrollTop = st;
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:12:23+00:00Added an answer on May 29, 2026 at 6:12 am

    You are increasing the rotation of the element on scroll, but you are not taking into account how far the user scrolled.

    You should re-write your code so that instead of always adding or subtracting one degree from the rotation, calculate the amount of rotation based on how far the user has scrolled. That way you can set beginning and end points where your element will always be at the same rotation at those points.

    Update

    A simple example of what I’m talking about is here:

    $(window).scroll(function(event){
       var srotate = "rotate(" + $(this).scrollTop() + "deg)";
       $("img").css({"-moz-transform" : srotate, "-webkit-transform" : srotate});
    });
    

    Here is a demo: http://jsfiddle.net/EnSkJ/4/

    Since this code rotates the element based on the scroll position, the rotation will always be the same at the same scroll position.

    Update

    Also you can detect what browser is being used and only update the proper vender-prefixed transform property:

    var venderPrefix = ($.browser.mozilla) ? '-moz-' :
                       ($.browser.webkit) ? '-webkit-' : 
                       ($.browser.msie) ? '-ms-' : 
                       ($.browser.opera) ? '-o-' : '';
    
    $(window).scroll(function(event){
       $("img").css(venderPrefix + "transform", "rotate(" + $(this).scrollTop() + "deg)");
    });
    

    Here is a demo: http://jsfiddle.net/EnSkJ/6/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an answer to another question it was mentioned that passing XML as a
In a comment on this answer to another question , someone said that they
In another question, the answer states that on Unixes with /proc , the really
Edit: From another question I provided an answer that has links to a lot
Just a simple question: I have read that a class should be made static
Per a great answer from another question I have begun mounting global resources (css/js/images)
I saw this in an answer to another question , in reference to shortcomings
The answer to another SO question was to use this SQL query: SELECT o.Id,
In another question, the accepted answer shows a method for reading the contents of
In another question , the accepted answer suggested replacing a (very cheap) if statement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.