Navigate through the page and once you reach a specific point, scroll the element

I'm feeling a bit stuck on how to achieve this particular effect.

Imagine a web page where, as a user scrolls down, a specific element responds to the mouse scroll input by appearing to move along with the scrolling motion. Once that element reaches a certain position (like top: -500), I want the scroll action to revert back to moving the main webpage content again. Any ideas on how I can implement this?

I'm currently working on a demo in a code sandbox, but so far I haven't had much success. I'll share the link once I have something to showcase.

Update: Here's a glimpse of a potential solution or pseudo code https://jsfiddle.net/vk0jk37v/23/

I've also included an image displaying one area where I plan to use this scrolling effect.

//pageFeature.style.backgroundPosition = "0px " + parseInt(-y / 6) + 'px'); 

var element = document.getElementById('container').getBoundingClientRect();
var elementTop = element.top //distance from top 720;

// flag to prevent repeated function calls during scrolling on the image
var isScrollingImage = false;

// function to disable scrolling on the body
var disableScroll = function() {
    document.body.style.overflow='hidden';
}
// function to enable scrolling on the body
var enableScroll = function() {
    document.body.style.overflow='auto';
}
// adjust background position along the y-axis based on scroll movement
var scrollImage = function() {
    console.log("called");
   isScrollingImage = true;
   var pageFeature = document.getElementById("inner");
   var pageFeaturePosition;
   pageFeature.style.backgroundPositionY=parseInt(-scrollY / 10) + "px";
    //if (background is scrolled to bottom) {
    //    enableScroll();
    // }
}

// trigger scrollImage() when the element reaches the center of the viewport and no scrolling animation is already applied
function checkPosition() {
    if (scrollY > 720 && !isScrollingImage) {
        disableScroll();
        scrollImage();
    }
    //isScrollingImage = false;
}

// ensure that the image moves back down once it's out of view, 
// scrolling animation should only occur on the way down

document.addEventListener('scroll', checkPosition);

Answer №1

    let thresholdY = 150; 
    let thresholdCounter = 0; 
    let speed = 4; 
    let element = document.getElementById('yourElement'); 

    window.onscroll = function(event) { 
      if(scrollY > thresholdY) { 
        window.scrollTo(0, thresholdY); 
        element.setAttribute('style', 'transform:translate3d(0,' + (--thresholdCounter * speed) + 'px,0);'); 
        return false; 
      }
      return null; 
    }; 

Experience it yourself: https://jsfiddle.net/pkt6xnb5/

The concept behind this code is to freeze the window once the user hits a specific Y position and simultaneously move the element in the opposite direction as they scroll further down. Does this meet your requirements?

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

Adjust the transparency of CSS elements using a jQuery selector

I am developing a grid of brand thumbnails with interactive icons. When one of the icons is hovered, I want all the icons to change opacity and become visible. Here is the current HTML structure: <div id="brands-wrapper"> <img class="brands" ...

Identifying duplicated video duration data associated with videoIds in asynchronous calls using Youtube API v3

I've encountered an issue with the asynchronous methods that retrieve and display all vidDuration values followed by the corresponding viewCount values for each videoId. The problem arises when the vidDuration value repeats only the last received one ...

What are the best practices for maximizing the efficiency of bootstrap-sass?

Currently, I am exploring npm modules and came across bootstrap-sass. After downloading the modules, I was seeking a solution to compile scss into the static folder of my application, as well as the js bootstrap files. However, after checking the npmjs do ...

The click function may need an additional click to complete its execution, but ideally, it should be accomplished in just one click

I have a script that filters elements on a webpage by checking the data attribute of the clicked element against class names on the filtered items (.filter-boy). As the elements are categorized into Categories and Subcategories, I aim to hide any parent c ...

Creating a dynamic dropdown menu in Laravel 5.3 - A step-by-step guide

Here is my HTML code: <div class="form-group has-feedback{{ $errors->has('kdkotama') ? ' has-error' : '' }}"> <select class="form-control" name="kdkotama" id="kdkotama"> <option value="">---- ...

A dynamic image carousel featuring multiple images can be enhanced with fluid movement upon a flick of

I am trying to enhance this image slider in HTML and CSS to achieve the following objectives: 1. Eliminate the scroll bar 2. Implement swipe functionality with mouse flick (should work on mobile devices as well) 3. Make the images clickable .slider{ ove ...

How can Spring and AngularJS (HTML) work together to retrieve the Context Path?

Currently, I am working with Spring and AngularJS. I haven't encountered any issues displaying my index.html using Spring in the following way: @RequestMapping(value="/") public String index() { return "/app/index.html"; } Is there a way for me ...

Exploring td data inside tr element using LXML

Is there a better way to parse individual statistics from the Yahoo Finance tables for formatting purposes? The current method involves repeating code to retrieve stats within each row of the table, as shown in the example below. It seems inefficient - a ...

Tips for transferring a request response to another request:

In my ExpressJS application, I am utilizing the request-promise module to send two requests. The catch is that I need the response data from the first request to be transferred to the second request. Here is an illustration of what I am aiming for: const ...

Accessing Private Files with Signed URLs from AWS S3

Issue: The challenge is to securely allow users to upload a file and retrieve it later. The files are stored in private Buckets and objects using S3 pre-signed URLs for uploading. However, fetching the file poses a problem as the signed URLs expire after ...

Interact with SOAP web service using an Angular application

I have experience consuming Restful services in my Angular applications, but recently a client provided me with a different type of web service at this URL: http://123.618.196.10/WCFTicket/Service1.svc?wsdl. Can I integrate this into an Angular app? I am ...

What is the best way to search a map object that serves as the document ID in Firebase?

I am attempting to retrieve all fieldnames within the payload > (random doc id) objects. https://i.sstatic.net/y9703.png At this moment, my approach involves fetching other collections using the following code: async fetchPage() { const query = fir ...

Attempting to persist a nested document in MongoDB using mongoose within a Nodejs environment

I attempted to save an address as a nested document in the following format When sending data from the client side, it was formatted like this: const address = JSON.stringify( { addressline1:form.addressline1.value, addressline2:form.addressline2.value, c ...

Check if the element is not present in the array, then add it to the array

I am attempting to generate an array in JavaScript with unique elements extracted from a JSON feed. My thought process is possibly too influenced by PHP, where a simple script like the one below would suffice: $uniqueArray = array(); foreach($array as $ke ...

Full scale intricate grid arrangement

Is there a way to make this header 'full width' so that the image appears on the left side of the screen while keeping the content in the grid? https://i.sstatic.net/U6pft.png I was considering creating a new container. .container--full { max- ...

When forcibly closing a window, the disconnect event for Socket.IO sockets may not trigger as expected

Currently in the process of creating chat rooms with Socket.io. I've had good success so far, as it's user-friendly and well-documented. However, I've noticed that if I reload the page, wait for the socket to connect, and then close the w ...

Exploring the power of Angular's ng-repeat to generate hierarchical lists

I am looking to display a structured list of layers and sublayers by looping through an object using ng-repeat. var lyrslist = [ { layername: "Base Maps", layertype: "layer" }, { layername: "Open Street Map", layertype: "sublayer" },    { layer ...

Trying out REST endpoints using curl's -X PUT command results in a 404 error code

Currently, I am delving into the realm of the MEAN stack and following along with the MEAN tutorial provided by thinkster.io. You can find the tutorial here: As of now, I am navigating through the "Opening REST Routes" section. In an attempt to PUT an upv ...

Having difficulty in getting a hyperlink to open in a new tab with _blank attribute

I'm currently working with this code for my link. <a href="https://dribbble.com/jamesfrewin"><img class="socialicon" src="images/icons/dribbble_dark.png" onmouseover="this.src='images/icons/dribbble_pink.png'" onmouseout="this.src= ...

Implementing jQuery during the navigation between Node routes

Below is a snippet of my jQuery code: $(function () { $('.mnav a').click(function () { el = $('.border'); el.addClass('blink'); el.one('webkitAnimationEnd oanimationend msAnimationEnd animatio ...