browse through the percentage of the display

  • Is there a way to use jQuery to determine when the user has scrolled 50% of the screen? (For example, scrolling 50px)
  • After detecting this scrolling milestone, how can we smoothly animate the page to #second, or effectively move to the top at 100%? (There seems to be a strange behavior during the animation)

Check out the example here: http://jsfiddle.net/NH6Km/2/

JQUERY:

$(function(){   
$(window).scroll(function() {
  if ($(window).scrollTop() > 50) { 
    ('body,html').animate({ scrollTop:             
    $('#second').offset().top }, 1500); 
  } 
});     
})

HTML:

<div id="first"></div>
<div id="second"></div>

CSS:

#first{
    position:absolute;
    width:100%; height:100%;
    background:blue;
} 
#second{
    position:absolute;
    top:100%;
    width:100%; height:100%;
    background:yellow;
}

Answer №1

Implementing the scroll event, as suggested by @thecodedestroyer, allows you to achieve a similar effect like this:

var currentSelector = "#first";
var selectorArray = ["#first", "#second", "#third", "#fourth"];

$(window).on("scroll", function (event) {
    var index = selectorArray.indexOf(currentSelector);
    if (index >= 0) {
        if (window.pageYOffset > $(currentSelector).offset().top) {
            currentSelector = selectorArray[(index + 1) % currentSelector.length];
        } else if (window.pageYOffset < $(currentSelector).offset().top) {
            currentSelector = selectorArray[(index - 1) % currentSelector.length];
        }
        $("body, html").animate({
            scrollTop: $(currentSelector).offset().top
        }, 0);
        event.preventDefault();
        return false;
    }
});

Test it out here: http://jsfiddle.net/cxJQE/

Answer №2

To achieve smooth scrolling effects, implement the scroll event with jQuery by referencing http://api.jquery.com/scroll/. Once the event is triggered, enhance the scrolling experience using this helpful plugin:

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

Displaying a modal following the closure of another modal in Bootstrap 3

In one of the modals on my webpage, there are two close buttons. The first button simply closes the modal (.btn-typoedit), while the second button (.btn-newcontact) not only closes the current modal but also opens another one immediately (#change-contact). ...

Enhanced Floating Elements with jQuery on WordPress

I've inserted this code into my website (excluding the sections for configuring comments and headers): http://jsfiddle.net/WzLG2/3/ Here is the JavaScript snippet: jQuery.noConflict(); jQuery(document).ready(function() { var top = jQuery('#smi&a ...

Tips for ensuring the counter displays numbers incrementally instead of all at once

Is it possible to make the counters count sequentially (from left to right) instead of all at the same time? Here is the code for my counter: (function($) { $.fn.countTo = function(options) { options = options || {}; return $(this).each(funct ...

tap the hyperlink to complete the form and mimic the action of a designated

I'm working on an HTML form that includes a table and two submit buttons. <input type="image" name="button1" value="First Button" src="some_image.png"> <input type="image" name="button2" value="Second Button" src="some_image.png"> One ch ...

Tips for effectively resizing an iframe within a grid layout

Task Tailwind React When adjusting the scale of an iframe, it disrupts its position within the grid. Expected Outcome: The iframe should maintain its position after scaling, but with a smaller size. Current Behavior: The iframe's position is be ...

Parsing JSON with jQuery in Grails is a seamless process that allows for efficient

I've implemented an ajax function that successfully sends data to my controller. var ids = grid.jqGrid('getGridParam','selarrrow'); if (ids.length>0) { var names = []; for (var i=0, il=ids.length; i < il; i++) { ...

"Troubleshooting issue in Django 1.6.2 where CSS styles are not being applied to

I am encountering an issue with this code and would greatly appreciate some assistance. The base.html file contains a {% block content %}{% endblock %}. I have created a Signup.html file structured like this: {%extends 'base.html'%} {% block co ...

Customize the behavior of jQuery cycle with an <a> tag

Can the jQuery cycle be accessed through a link in order to override the i variable? I've come across examples that can achieve this, but not in a scenario like this where the cycle() function is located within a variable: $(document).ready(function ...

Is there a way for me to update the value by simply clicking on the right arrow?

How can I display the next element of an array in the input area when clicking on the right arrow? $(function(){ var room = ['1-visiting office', '2-', '3-']; $('#text_holder').val(room[0]); $( ...

How to retrieve the value of a td element using jQuery when it is nested inside a

Is there a way to retrieve the td value of an element inside a td within a table with the class t2? I attempted the following code, but it did not work. var test = $(.lv1 tr:nth-child(" + row2 + ")").children("td:last").children(".lv2 tr:last").children(" ...

Progress bar display not available

I have recently developed a JavaScript quiz application and included a progress bar feature. While it works flawlessly offline on my laptop, I encountered an issue when uploading the files to Codepen.io - the progress bar fails to display. I would appreci ...

Divide data into an HTML table and merge it with header information

My HTML form contains an interactive HTML table where users can add/delete rows. I am sending this data to a Google Sheet and need to store the row information with corresponding header details. Each time a user submits the form, a new row is added to the ...

When converting a MySQL query to JSON in PHP, only the final result is returned

In my PHP code, I am encountering an issue where a query encoded to JSON is only returning the last result, even though the query should be returning multiple results: $counter=0; $reply=array(); $result = mysqli_query($conn,$stringQuery);; if ($result-&g ...

Safari IOS experiencing issue with element disappearing unexpectedly when input is focused

I am facing a situation similar to the one discussed in the question (iOS 8.3 fixed HTML element disappears on input focus), but my problem differs slightly. My chatbox iframe is embedded within a scrollable parent, and when the iframe is activated, it exp ...

How can I create a custom elevation for a Vuetify v-menu?

I'm currently working with vuetify and v-menu as outlined in the official documentation here https://vuetifyjs.com/en/components/menus/ I'm struggling to figure out how to apply elevation only on the bottom left and right corners. When I add a ...

The code functions properly within the emulator, however, it fails to execute on an actual device

Yesterday, I posted a question about the background related to this topic: on click event inside pageinit only works after page refresh. I received an answer for my question and tested it in Chrome devtools where it worked perfectly. However, today when ...

Conceal and reveal with finesse: Managing the Chosen Plugin CSS in

<script> $(document).ready(function(e) { $("#b").hide(); $(".chosen").chosen(); $("#a").click(function(){$("#b").show();}); }); </script> <p id="a">aaaa</p> <div id="b"> <select class="chosen"> <option& ...

Conceal objects in reverse sequence

My goal is to clear notifications grouped on a mobile OS style interface when clicking the "X" button. They should be removed in reverse order with a 1-second delay between each. Here's what I've achieved so far: $(document).ready(function () { ...

The jQuery AJAX form was successfully sent, but the `var_dump($_POST)` function returned no data

Hey everyone, I'm facing a simple problem but I'm really confused about it for an hour now. So, in a PHP page, here's the code snippet: <script> $( document ).ready(function() { $("#date_avant").click(function(){ $.post( "_corp ...

"Transferring an array of data to a Laravel controller via AJAX: A step-by-step guide

Is there a way to send arrays of data to the back-end all at once? The Problem 1. This is what I'm currently sending: array:5 [ "_token" => "S5s5ZTTnnP93MyXgCql0l9vhHsiqt5VWaFyEedXj" "product_id" => "21" "specification_id" => "6" " ...