Navigate to the following div elements using the mousewheel in jQuery

Looking to implement a parallax effect where the page scrolls to the next block on each mousewheel rotation.

I attempted to achieve this using the code below but it didn't work as expected. Can someone offer any suggestions?

Check out the live JSFiddle demo, (I've also included the mousewheel plugin)

$(document).ready(function() {
  $('section').css({
    'height': (($(window).height())) + 'px'
  });

  // Now bind the event to the desired element
  $('section').bind('mousewheel', function(e) {
    if (e.wheelDelta / 120 > 0) {
      $(this).text('scrolling up !');
    } else {
      $(this).text('scrolling down !');
    }
  });
});

$(window).resize(function() { // On resize
  $('section').css({
    'height': (($(window).height())) + 'px'
  });

});
.container-fluid {
  padding: 0;
}
section {
  float: left;
  width: 100%;
  position: relative;
}
.screenOne {
  background-color: rgba(0, 0, 0, 0.7);
}
.screenTwo {
  background-color: rgba(0, 0, 0, 0.6);
}
.screenThree {
  background-color: rgba(0, 0, 0, 0.4);
}
.screenFour {
  background-color: rgba(0, 0, 0, 0.3);
}
.screenFive {
  background-color: rgba(0, 0, 0, 0.2);
}
.screenSix {
  background-color: rgba(0, 0, 0, 0.1);
}
h1 {
  text-align: center;
  color: #ff0000;
  margin: 25% 0 0 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.13/jquery.mousewheel.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
  <section class="screenOne">
    <h1>One</h1>
  </section>
  <section class="screenTwo">
    <h1>Two</h1>
  </section>
  <section class="screenThree">
    <h1>Three</h1>
  </section>
  <section class="screenFour">
    <h1>Four</h1>
  </section>
  <section class="screenFive">
    <h1>Five</h1>
  </section>
  <section class="screenSix">
    <h1>Six</h1>
  </section>
</div>

Answer №1

Here is a functioning solution: https://jsfiddle.net/jtpbq9zf/

If you encounter the issue of event.wheelDelta returning undefined, refer to event.originalEvent.wheelDelta

$(function(){
  $('section').css({'height':(($(window).height()))+'px'});
  // Bind the event to the desired element
  $('section').on('mousewheel', function(e){
    e.preventDefault();
    if(e.originalEvent.wheelDelta < 0) {
      if (!$(this).is(':last-child'))
        $('body').scrollTop($(this).next().offset().top);
    } else {
      if (!$(this).is(':first-child'))
        $('body').scrollTop($(this).prev().offset().top);
      }
  });
  $(window).resize(function(){ // On resize
      $('section').css({'height':(($(window).height()))+'px'});
  });
});

Improved Smooth Animation

Instead of using

$('body').scrollTop($(this).next().offset().top);

try utilizing

$('body').animate({scrollTop:$(this).next().offset().top}, '700');

Also, pay attention to the prev() and next() elements.

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

Utilize Flexbox to create ample space on the right side

Hi there, I've noticed that when I use display: flex, the right side of my row seems to have a slight gap. How can I go about fixing this issue? It's not very obvious in jsfiddle, but you can spot it using Firebug or a similar tool. Check out th ...

What is the best way to maintain the original font color even after hovering over it?

Hello everyone, I'm having trouble maintaining the white color on hover. When I move the cursor to the next element inside the hover element, the font color changes. I've tried various methods to keep the desired color but nothing seems to work. ...

Using Placeholder in JavaScript for Multiple Form Validations

I am currently facing an issue with my form validation. It checks each input to determine if it is valid or not, but the problem arises when only one input is valid, as all inputs are then considered valid. I have tried moving the return true statement, bu ...

JavaScript functions may become unresponsive following ajax loading with tables

I have a table that is automatically generated by a database using PHP with four rows. The last row (4) has a button that triggers the function below, and this is repeated for every line. After clicking the button, the script sends a request to update the ...

Why is it that my jquery code seems to be struggling with calculating the count accurately?

I'm currently working on restricting keyword input on my website. My goal is to automatically add a 'span' tag to any keyword entered by a user, as shown in the example html code below. Although I can limit the number of words, the count i ...

jquery-tokeninput not populating field with content

I have set up a form where users can create a profile and select keywords from an autocomplete token field using jQuery tokenInput. However, I'm facing an issue where the text in the token field is not getting submitted when the form is sent. I am usi ...

The issue of transform scale not functioning properly in conjunction with background clip and gradients in CSS

Looking to transform a div with the transform: scale(-1,1) property while using background-clip: text on the text within it. However, this causes the text to disappear. Here's what I've attempted: .reverse { transform: scale(-1, 1); } .gr ...

Creating an art exhibit in an illuminated amp lightbox using Django

Currently, I am immersed in a project that involves Django on the back-end and amp on the front-end. However, I am facing some challenges in connecting both tags, particularly when it comes to implementing a lightbox feature. What I aim to achieve is a li ...

What is the process for importing jQuery UI with ES6/ES7 syntax?

Struggling to integrate jQuery UI functionality into my reactJS/Redux application. I have installed both jQuery and jQuery UI using: npm install jquery jquery-ui Then, attempted the following imports: import $ from 'jquery'; import jQuery from ...

Issues with the count up functionality in jQuery

I'm currently working on a project involving countups. My aim is to have multiple countups displayed on a single page. While having one countup using interval() function poses no issues, I encounter trouble when trying to display two or more countups ...

When an element is appended, its image height may sometimes be mistakenly reported as

I am dynamically adding divs and I need to retrieve the height and width of an image. Based on this information, I have to apply CSS to the MB-Container class. For example: if the image is portrait orientation, set container width to 100%. If it's ...

When CSS is applied, the canvas is stretched, whereas it remains normal when `width` and `height` attributes are used

I own two canvases, one is sized using HTML attributes width and height, while the other is sized using CSS: <canvas id="canvas1" width="300" height="300" onmousedown="canvasClick(this.id);"></canvas> < ...

Conceal a request URL within JavaScript through the use of Laravel/Ajax

I have an idea that I'm not sure is great or even feasible, but I really need to make it work. I am attempting to obfuscate a URL that needs to be used in a Javascript function as a parameter. This is what I currently have: <script> myFunction ...

Uncovering the row ID and dropdown value within a gridview during drop change events

I am populating a grid view with data using a stored procedure. The gridview contains a dropdown (status) and when the user changes the value in the dropdown, it should update the database... Issue: How can I retrieve the id of a specific row along with t ...

The Ajax sign up request for the Express route is failing with a POST error code 404 at http://localhost:300

When I make an Ajax post to my express /signup/ route, I receive a POST 404 error for http://localhost:3004/signup/. Any assistance would be greatly appreciated as I am new to node! server.js const pg = require('pg') const bodyParser = require( ...

The object for checking Ajax connections is not providing any values

I've been working on creating a unique AJAX connection check object, but unfortunately I'm struggling to get it to function properly. :/ Here's the object structure: function Connection(data){ this.isConnected = false, this.check = funct ...

Using jQuery for Ajax Requests in Rails

While attempting to send an Ajax Request using jQuery, I encountered a "405 Method Not Allowed" Error. The purpose was simply to post a form, extract the details from it, and insert them into the database - standard procedure. I am currently utilizing WEBr ...

Send a notification for every change made to a text field in Python

As a beginner in Python and web programming, I currently have functional HTML and Python code on app engine utilizing jinja2 and webapp2 libraries. This code retrieves query results based on user input in a text box followed by hitting the submit button. H ...

The click() function in jQuery executing only once inside a "for" loop

This is an example of my HTML code: <!DOCTYPE html> <head> <title>Chemist</title> <link href="stylesheet.css" rel="stylesheet"> </head> <body> <h2 id="money"></h2> <table border="1px ...

What is the best way to determine if a jQuery AJAX response contains HTML elements?

On my webpage, I have a single form that triggers an AJAX call which can result in two different responses. One of the responses only includes a status code. In order to display any HTML contents returned by the response object on my page, I need to exami ...