How can I make a div scroll diagonally? A regular scroll would go up/down or left/right, but I’d like to make the div scroll up and to the right or down and to the left.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
EDIT: as required by the OP, I’ve updated the example code to introduce a correction factor, required to change the diagonal movement direction and degree.
If you want to use jQuery you’ll have to download the MouseWheel plugin.
Then, you can write a simple function binded to the
mousewheelevent, such as:HTML
CSS
Alternative 1: JS using CSS top and left
Alternative 2: JS using offset()
Now you can move the box up&right by scrolling up and vice-versa by scrolling down.
In this example, on every
mousewheelevent, the callback function:topandleftCSS properties)mousewheelevent, so that scrolling up we have a negative delta, and scrolling down we have a positive deltaTo change degree and direction, just change
factorXand/orfactorYvalues, so that:Here’s a demo you can test.
Alternative 3: JS using cos() and sin()
In this example, what to change is the value in
angle(45 in this example). Everything else works just like the others examples.Last thing, if it’s required to change the velocity of the movement, just multiply
factorXand/orfactorYby the wanted coefficient (for example, 1.5 for one and half time of the velocity, or 2 for twice the velocity, etc.).It’s possibile to try it in a demo.
EDIT
Just for the sake of knowledge, you can reach the same goal using CSS Transform. This allows you to take advantage from GPU accelerated hardware. (Further informations can be found in the article of Smashing Magazine and Paul Irish).
HTML
CSS
JS
However, as you can see in this example, you’ll need to keep track of the element X,Y position, because it’s a little bit complicated to retrieve these values directly from the CSS. Moreover, this example is kept easier, but in production you’ll have to support every vendor specific CSS property (
-webkit-,-moz-,-o-,-ms-, etc.).Here’s a working demo (if it doesn’t work, you probably will have to edit the code according to the specific prefixed CSS property for your browser).
EDIT: Since the OP has seen that listening to the
scrollevent it was the better choice for him, I’ve added the relative code (only the JS code is reported here, since HTML and CSS are pretty the same as the first example):Here’s a working demo.
BONUS: IDEA
Instead of use common local variables for every element, HTML5
data-*properties could be used to store element’s data (for example: correction factor, last position, etc.), then jQuery.data()method could be used to retrieve these data and process them.PRO:
CONS: