What is the most effective way to achieve a seamless scrolling effect with jquery?

I'm attempting to create a smooth scroll effect using jQuery, and while I can technically achieve it, the movement feels clunky and awkward.

JS:

$(document).ready(function(){
    $(this).bind('mousewheel', function(e){
        if(e.originalEvent.wheelDelta / 120 > 0) {
            movePage();
        }
    });
    var page1top = 0;
    function movePage() {
        page1top = page1top - 1;
        // page1 is the page that i want to move up when people scroll down.
        $('#page1').css('top': page1top + '%');
    }
});

HTML:

<div id='container'>
 <div id='page1'></div>
</div>

CSS:

#container {
    position: absolute;
    height: 100%;
    width: 100%;
    overflow: hidden;
    z-index: -100;
}

#page1 {
    width: 100%;
    height: 500%;
    z-index: 0;
}

I'd like to find a way to make this scroll effect smoother without relying on external plugins. Any advice would be greatly appreciated!

Answer №1

If you are in search of a way to animate using jQuery, consider exploring the jquery animate method. A demonstration can be found in the provided fiddle.

$('.to-target').click(function(){
  $('html, body').animate({
    scrollTop: $("#target").offset().top
  });
});
.box {
  width: 100vw;
  height: 100vh;
}

.long {
  background-color: aqua;  
}

.short {
  background-color: beige;
  height: 100vh;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="boxes">
  <div class="long box">
    <button class="to-target">Go to Target</button>
  </div>

  <div id="target" class="short box">

  </div>
</div>

Answer №2

Swap out .css() for .animate() and smoothly scroll to the desired selector:

$element = $('#specificElement');
$('html, body').animate({
    scrollTop: $element.offset().top
}, 200);

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

Why does Grunt encounter an issue with cdnify, displaying a warning that says: "Caution: Parameters for path.join should be in string format"?

When running grunt in my Yeoman-generated Angular project, I encounter the following warning: Running "cdnify:dist" (cdnify) task Going through dist/404.html, dist/index.html to update script refs Warning: Arguments to path.join must be strings Use --forc ...

Clicking on a dynamically generated link will not result in the file being downloaded

I have a list of document names and IDs retrieved from a database, displayed in an unordered list like this: <ul id="list"> <li onmouseover="onHover(docNumber)"> <a herf="#" id="docNumber">DocName</a> </li> </ ...

What could be causing the incorrect styling of the <h1> tag and certain Bootstrap elements in my Vuejs project when I import Buefy?

In my webpack-simple Vue.js project, I am utilizing Bootstrap 4 and Buefy. However, upon importing Buefy as per the documentation, I noticed that my <tag does not resize the text correctly and the nav-bar in Bootstrap 4 is displaying the wrong width. T ...

Ways to align elements horizontally while preventing them from shifting positions

I'm faced with a layout challenge where I need to place 2 components side by side, but I want one component to stay on the right without affecting the orientation of the other component. Example: https://i.stack.imgur.com/T5Ram.png Currently, when I ...

CSS is not being applied correctly in Safari 13 following a resize

When using Safari, I have noticed that my styles are applied correctly upon page load for any screen size. However, if I resize the window from desktop to mobile and back again, the desktop styles do not get applied. To see this behavior in action, simply ...

Using Double Equal in a JavaScript For Loop

I'm struggling to comprehend why utilizing a double equals (or even a triple equals) in the condition of a for loop doesn't function as expected. Consider this example: for (i = 1; i == 5; i++){ console.log(i) } When I replace == with <= ...

The Ajax dropdown is failing to display any results

I created a script to filter through a database and retrieve all the records that contain a specific value. I have set up an AJAX dropdown for selecting the value, but when I make a selection in the dropdown, it does not show any results. Any assistance on ...

Modifying the function name or element ID in a HTML template can cause it to cease functioning

I'm currently working on developing an app using Django and I've encountered a strange issue. In my template, I have the following code: <div class="form-group"> <input name="Data_inserimento_entry" type="date" class="form-control" i ...

Retrieve the smallest value from an array of objects using BitGO

Bitgo stores all transactions as objects within a large array. Within the nested .entries, we can identify that the first TX object contains two negative values -312084680 and -4254539, of which I only require the lowest value. My current code successfully ...

Store a new JSON item in the localStorage

Currently, I am tackling a task in Angular where the objective is to store items to be purchased in localStorage before adding them to the cart. There are four distinct objects that users can add, and an item can be added multiple times. The rule is to ch ...

The elements within the array are being refreshed accurately, however, a separate component is being removed

I have developed a component that has the ability to contain multiple Value components. Users can add as many values as they want, with the first value being mandatory and non-removable. When adding two new Value components, I provide input fields for name ...

Jquery results in an error and causes CSS elements to become stuck

Here is the code I am currently using on JSFIDDLE: JSFIDDLE The issue I am facing is that when an item is dragged to a correct box, it disables the original item from being dragged again. However, this causes the CSS to stick, preventing the green color o ...

Adjusting the widths of <input> and <select> elements

I am facing an issue with aligning an input text box under a select element, where the input is intended to add new items to the select. I want both elements to have the same width, but my attempts have not been successful (as I do not wish to specify diff ...

Creating a contact form using HTML and PHP

Hey there, I'm new to web development and I've been trying to implement a contact form on my website using some source code. Unfortunately, it's not working as expected and I could really use some help. Here is the code snippet: HTML & ...

Monitoring every request in Express.js(logging)

My web application is built on node and express for the backend. With a large number of users, debugging using console logs can be confusing due to messy logs from different users. Is there a way to track logs based on requests (like a Request ID)? ...

Express Router Setup Error: The Router.use() method is expecting a middleware function, but instead received an undefined value

First and foremost, I want to clarify that I am well aware of the plethora of questions posed on this platform with a similar title. I have already attempted the suggestions provided there, but unfortunately, they either did not work for me or were too sp ...

Determine the distance traveled by the mouse along a vector using Three.js

I am currently working on a project that involves determining the distance traveled by a mouse along a normal vector. The goal is to adjust a group of vertices within an object by moving them along the normal vector of an intersecting face. My current se ...

What is the best way to position a <label> to the left of an <input> field?

Examining the HTML and CSS code below: * { box-sizing: border-box; } .field { overflow: auto; } .field label { float: left; width: 80px; } .field input { display: block; width: 100%; } <div class="field"> <label>Test< ...

The Image Properties dialog in CKEditor appears to be empty

Currently, I am utilizing the default Image plugin with the standard settings: CKEDITOR.replace( 'editor', { filebrowserBrowseUrl: '/app/myimages.html', filebrow ...

Utilizing a two-column design, position the third sibling element without relying on margins

I am currently working on a two-column layout. The left column (header) is set to be sticky/fixed, while the right column remains static. In my HTML structure, I have 3 sibling elements (header, div.content-column, footer). The footer is positioned below t ...