Animate horizontal scrolling with jQuery when the user scrolls the mouse

I've been experimenting with an animated horizontal offset for a div element triggered by the mousescroll event. You can check out my progress so far by following this link - could someone please take a look and help me identify any issues?

Currently, the animation is activated on click action but I would like it to be triggered by mousescroll instead. http://jsfiddle.net/laksdesign/WvRR6/

jQuery(document).ready(function($) {

    $(".scroll").click(function(event) {
        event.preventDefault();
        $('#wrapper').animate({ scrollLeft: $('#wrapper').scrollLeft() + $(this.hash).offset().left }, 800);
    });

    $('body').bind('mousewheel', function(scroll){
        if(scroll.originalEvent.wheelDelta /120 > 0) {
            scroll.preventDefault();
            $('#wrapper').animate({ scrollLeft: $('#wrapper').scrollLeft() + $(this.hash).offset().left }, 800);
        } else{

        }
    });
});

I found inspiration from another example that uses mousescroll-based animations: http://jsfiddle.net/laksdesign/EATaU/

Answer №1

If you want to capture the scroll event for a mouse in jQuery, it's best to use a plugin or an external function rather than trying to create your own solution. The reference provided includes a fiddle that uses a "mousewheel.js" file as the necessary plugin.

For more information on how to implement the plugin, check out this answer.

One example of a plugin you could utilize is:

  • BrandonAaron.com - Search for this site using Google since direct linking is not permitted.

Here's how you would use the plugin:

// using bind
$('#my_elem').bind('mousewheel', function(event, delta) {
    console.log(delta);
});

As recommended by user @Dom, another popular plugin you can consider is:

  • jQuery Mousewheel

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

Achievement with ajax: If the status code is 200, execute one function; otherwise, execute a

I can't figure out why this isn't working... I'm using $.ajax to run file.php and pass a POST value from an input field. Even though the file.php is functioning properly (it successfully adds a user to my newsletter), my ajax function seems ...

Attempting to sort through elements in JavaScript

I'm looking to filter specific films based on choices made in the dropdown menus below. <select id="filmDropdown"> <option value="0">All Films</option> <option value="1">Film 1</option> <option ...

What is the best way to automatically increase the value of another column in SQL when the primary key is set to auto

In my database, I currently have a table storing customer details. I am looking to introduce a new column that will provide either existing or new customers with a unique auto-incremented ID for identification purposes. Additionally, I require a primary ke ...

Enhancing File Upload functionality with Struts 2 and jQuery

As a newbie to Struts2, I am attempting to upload a file in Struts 2 using the Struts2 jQuery plugin. My goal is to avoid page refresh during the file upload process. I understand that Struts 2 jQuery is supposed to facilitate AJAX requests to the action w ...

Flexbox: Arrange two items horizontally without setting a maximum width

I'm not sure if it's possible with flexbox, but I am looking to create a list with two items displayed side by side where the width of each item adjusts based on the content. Here is an example: <div class="items"> <div class="item"&g ...

Using Jquery to handle ajax responses through the load() function

Currently, I am working on implementing a comment voting system. In the process, I have a specific page called profile_new.php?id=194 This page utilizes jquery tabs to load an external file named sketch-comments.php?id=194&thumb=images/thumbs/1.png ...

CSS image cropping is a technique used to adjust the size

I have successfully created a grid of images with two columns, but I'm facing an issue with a portrait image disrupting the layout. I am looking for a way to adjust or "crop" the image so it matches the height of the landscape ones. I attempted to use ...

Utilizing AngularJS to iterate over a single extensive unordered list

In my current Angular app, the JSON structure is as follows: 0: Object $$hashKey: "004" Date: "2014-04-17" Items: Array[3] 1: Object $$hashKey: "005" Date: "2014-04-18" Items: Array[3] 2: Object $$hashKey: "006" ...

Troubleshooting a CSS problem on a table filled with images - See attached screenshots for

I've been working on a project involving my phpmyadmin database generating a series of 6 images on my website. I've placed them in a table, but now I'm facing some challenges - even though it seemed simple at first! The goal is to display t ...

CSS3 animations with background-image for Firefox are an exciting way to enhance the

I am currently working on a keyframe animation project using CSS. The animation is running smoothly in Chrome with supported -webkit syntaxes. @-webkit-keyframes title_toggle { from { background-image:url(images/title.png); } 75%{ background-image:url(im ...

Using CakePHP in combination with the ajaxForm plugin allows for dynamic loading of specific parts

I am using a jQuery plugin called AjaxForm to submit my forms to the server. Here is a simplified version of my setup: <div id="output1">...My CakePHP PHP form code goes here...</div> <script type="text/javascript"> $(document).rea ...

Avoid applying styles to the entire div

Is it possible to create a not selector in a stylesheet to prevent styling anything inside a div with the class myDiv? In my specific scenario, I am referring to tables, th, tr, and td elements within that div. table:not([class='myDiv']) thead ...

How can I obtain the progress of a jQuery ajax upload?

$.ajax({ xhr: function() { var xhr = new XMLHttpRequest(); //Track upload progress xhr.upload.addEventListener("progress", function(evt){ if (evt.lengthComputable) { var percentComplete = evt.loaded / evt.total; //Hand ...

json request not functioning properly in Internet Explorer and experiencing difficulties with caching

Unfortunately, the code snippet provided below is compatible only with Firefox and not Internet Explorer. The JSON file breaks when it encounters the text "Meanscoil na mBraithre Criostaí": "2028425":[19, "Awaiting Correction", "", "Meanscoil na mBra ...

Revise website design to adjust dynamically according to screen dimensions

Currently, I am working on a school project with a website created by former students. Unfortunately, I have limited knowledge of HTML and CSS which has made it challenging for me to ensure the site is mobile-friendly. There are issues such as text overflo ...

Measuring the height of a div element using Vuejs

Let me explain my current situation: I am trying to determine the height of a DIV component in order to dynamically inform its child component about its size. I have been working on implementing manual loading mode, but I have encountered an issue where t ...

What causes the "v-col" width to suddenly alter after adding the "bg-red rounded" class?

I'm struggling to organize the data into blocks using Vuetify. When I apply the "bg-red rounded" class to the "v-col", it alters the width of the column, resulting in an undesired grid structure. Here is the template section of my Vue file. Some data ...

CSS styling failing to meet desired expectations

Two different elements from different panels are using the same CSS styles, but one displays the desired text while the other does not. The CSS file in question is named grid.css. To see the issue firsthand on my live website, please visit: I am uncertai ...

variations in menu functionality on firefox

Could you please take a look at the links below? It appears that there may be an issue with FireFox. The green top menu seems to be displaying incorrectly. In Chrome/IE, it appears in one line, but in FF, the last two links are on the second line despite s ...

Utilize multiple background images in CSS with a set of three unique images

I have inserted three images into the background of my website's body. Two of them are displaying correctly (one with repeat-y), but the third one is not working. I've tried searching a lot but couldn't find a solution. body { backgr ...