How can I define the boundaries for the scrolling of a static background image?

Is there a way to limit how far a fixed background image can scroll down a page? Essentially, is it possible to change the background attachment to static when the bottom of the background image is 10px away from the bottom edge of its div container?

In the code snippet below, how can I make the coral gradient (placeholder for an image) stop scrolling with the page once it reaches 10vh away from the border between the red and yellow divs? (Preferably using CSS only, but open to JavaScript solutions as well)

.red {
  position: absolute;
  top: 0;
  height: 100vh;
  width: 100vw;
  background: red fixed linear-gradient(lightcoral, lightcoral) 45vw 20vh / 20vh 20vh no-repeat;
}

.yellow {
  position: absolute;
  top: 100vh;
  height: 100vh;
  width: 100%;
  background-color: yellow;
}
<div class="red"></div>
<div class="yellow"></div>

Appreciate any assistance!

Answer №1

Hello Aidan!

In my method, I utilize an img tag instead of using it as a background. The trick lies in employing the Position property with sticky positioning.

<style>
    body{
        margin: 0;
    }
    
    section{
        height:150vh;
        width: 100%;
    }
    
    .blue {
        background: rgb(23, 59, 71);
        padding: 10px 0;
    }

    .green {
        background-color: rgb(29, 82, 29);
    }

    .img{
        width: 100px;
        height: 100px;
        background-color: lightcoral;
        position: sticky;
        top: 50vh;
    }

</style>

<!-- body -->

<section class="blue">
    <div class="img"></div>
</section>

<section class="green"></section>

To adjust the border offset, you can experiment with adjusting the padding of its container.

I hope this solution is beneficial to you. Happy coding! ;)

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

Incorporate the ability to display a shape on a map when hovering over a table element, without the need to manually code a JavaScript function for every instance

I came across a script online that allows me to hover over text and have a shape appear on an imagemap. It's functional, but only works for a single instance. Is there a way to implement a JavaScript that handles individual instances so I don't h ...

Integrate functionality to track elapsed hours in stopwatch timer

Struggling to incorporate hours into my JS Stopwatch timer, the math for calculating the hours section is proving difficult. This is what I have so far, but I suspect the issue lies within the h variable. function formatTime(time) { var h = m = s = ...

Extracting PartialView contents as a string

I am looking to embed HTML code that is rendered by a PartialView. For instance, var partView = PartialView("myView", myModel); string content = ??; What should I replace the question marks with? ...

Tiny adjustment to jQuery Collapsible Panel

I have successfully implemented a jQuery Accordion in my demo. When I click on 'About Us', it reveals the 'Team' link below. Exciting! Now, I am wondering if it is possible for this Accordion to function without needing the 'item& ...

Improprove the efficiency of flattening an array containing nested JSON objects using JavaScript

Supposed to be an array structured like this: [ { "key_set1": { int_val: 3, arr_val: [ 1, 3, 4 ] } }, { "key_set2": { stri ...

Guide on centering an unfamiliar element in the viewport using HTML, CSS, and JavaScript

In the process of developing my VueJS project, I have successfully implemented a series of cards on the page. However, I am now facing a challenge - when a user selects one of these cards, I want it to move to the center of the screen while maintaining its ...

Automatically install modules during the execution of the Node Webkit build process

After developing a Node Webkit application, I used NW-Builder to generate the run files. The app's size ended up being quite large at 200MB due to the numerous modules utilized. My question is whether it is feasible to create an installer that will f ...

Retaining the data retrieved from a successful $.ajax call's done() function

After successfully retrieving the desired information in the done method, I encountered an issue when trying to assign it to a variable with broader scope. This approach had worked fine with $.each(...), leading me to assume that it would work here too. v ...

Filter products by pressing the "Enter" key while using the search input in

I have a list of products and I want to only display products that have a description or name containing the typed word when enter is clicked on the search input. Here is what I tried in my Search component: const Search = (props) => { return ( &l ...

Deactivate the button until the input meets validation criteria using Angular

What I'm trying to achieve is to restrict certain input fields to only accept integer or decimal values. Here's the code snippet that I currently have: <input type="text" pattern="[0-9]*" [(ngModel)]="myValue" pInputText class="medium-field" ...

What are the best practices for utilizing pre-defined CSS classes in Vue.js libraries?

I don't have much experience with CSS, but I'm really eager to customize the appearance of my chart. The chart is generated by a vue.js library and comes with pre-defined CSS classes. However, I'm uncertain about how to access and modify the ...

Using jQuery to merge two JSON objects into a single table

I'm currently working on creating a table like this: <table id="list"> <th>ID</th> <th>Name</th> <th>Price</th> <th>Image</th> < ...

Steps for comparing two inputs and displaying a div:1. Obtain the values of

I am looking to create a basic JavaScript function that will show or hide an element based on whether two input values match. This needs to happen before the form is submitted. <form> <input type="number" name="some1" id="some1"> <input t ...

tips on toggling div visibility based on data filtering

<script> function toggleContent(str) { if(str == 'recent' || str == 'old' && !($(".main_content").is(':visible'))) { $(".main_content").show(); } else if( str == 'newComment') { ...

Looking to incorporate HTML and JavaScript to create two unique forms that can be submitted separately on a single webpage

If I can't find an answer, then I ask: There are two different forms on the page: one is the "ask for call" form (which appears on every page of the site) and the other is a simple "contact form" (specifically placed on the contact page). Both forms ...

Submitting a form using jquery in HTML

Below is the code I have created. It consists of a text-input field and a 'Search' button within a form. Upon clicking the search button (submitting the form), it should trigger the .submit function to carry out the necessary processing. HTML CO ...

What could be causing certain items in my Bootstrap navbar to be hidden on mobile screens?

When I view my website on mobile devices, only the logo appears in the navbar, not the other elements. Can you help me troubleshoot this issue? NavBar HTML Code: .navbar-logo { width: 93.75px; height: 23.5px; margin-left: 10rem; } .navba ...

Is Angular UI's data binding more of a push or pull mechanism? How can I optimize its speed?

Suppose I have a variable a that is displayed in HTML as {{a}}. If I then update its value in TypeScript using a = "new value";, how quickly will the new value be reflected in the user interface? Is there a mechanism that periodically checks all bound var ...

Implementing a Responsive Form on an Image Using Bootstrap

I am facing a challenge where I need to position a form on top of a Responsive Image. My goal is to ensure that the form is responsive as well and adjusts its size according to the image. As of now, the image is responsive but I am struggling to make the ...

New Angular Datatables used to create a revitalizing table

In my project, I am utilizing the Angular Datatables library. The data is fetched from a URL that returns a JSON object, which is then stored in an array and used to populate a table. appenditems(){ this.items = []; this.items2 = []; this.items ...