modify the final attribute's value

Hello I've been using skrollr js to create a parallax website. However, the issue arises when determining the section height, as it depends on the content within. My goal is to locate the last attribute and adjust the number value based on the section height.

For instance:

For section 1, the data-800 attribute will be

data-800="position:fixed;top:-$('section 1').height;"

For section 2, the data-900 attribute will be

data-900="position:fixed;top:-$('section 2').height;"

The ultimate objective is to dynamically set the height for each section in the attribute.

$('section').attr('data-800', 'position:fixed;top:-' + $('.section').height());
var s = skrollr.init({
    render: function(data) {
        console.log(data.curTop);
    }
});
html,
body {
  margin: 0;
  height:100%;
}

.grad0 {
    height:100%;
  position:fixed;
  top:0px;
  background-color: #FFAAAA;
    
 background-image: url(https://production-assets.codepen.io/assets/profile/profile-bg-8ff33bd9518be912289d4620cb48f21eb5c9a2e0b9577484126cfe10a5fb354f.svg);
}

.grad1 {
  position:fixed;
  height:100vh; 
  background-color: #00FF00
}

.grad2 {
  position:fixed;
  top:800px;
  background-color: #0000FF
}
.grad3 {
  position:fixed;
  top:1200px;
  height:400px;
  background-color: #00FFFF
}
section {
  width:100%;
}
img {
  position: absolute;
  top:150px;
}
<section class="grad0 w60" data-0="top:0px;" data-400="top:0px;"  data-800="position:fixed;top:-400px;">
  <img src="http://www.cs.cmu.edu/~chuck/lennapg/len_top.jpg" data-0="left:0px" data-400="left:1600px;">
</section>
<section class="grad1" data-400="position:fixed;top:638px;height:100vh;" data-900="position:fixed;top:-400px;">
    <h1>Hello World</h1>
</section>
<section class="grad2"  data-0="position:fixed;top:800px;" data-400="position:fixed;top:800px;height:100vh;" data-800="position:fixed;top:000px;">
    content
</section>
<section class="grad3"  data-0="position:fixed;top:1200px;" data-400="position:fixed;top:1200px;" data-800="position:fixed;top:400px;">
    content
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/skrollr/0.6.30/skrollr.min.js'></script>

Answer №1

Updated:

Please ensure that each data attribute is targeted in your comments:

$('section').each(function() {
  var $this = $(this)
  var dataset = Object.keys(this.dataset);
  $.each(dataset, function(i, item) {
    if (i === dataset.length - 1) {
      $this.attr('data-' + dataset[i], 'position:fixed;top:-' + $this.height());
      console.log($this[0].dataset);
    }
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="grad0 w60" data-0="top:0px;" data-400="top:0px;" data-800="position:fixed;top:-400px;">
  <img src="http://www.cs.cmu.edu/~chuck/lennapg/len_top.jpg" data-0="left:0px" data-400="left:1600px;">
</section>
<section class="grad1" data-400="position:fixed;top:638px;" data-900="position:fixed;top:-400px;">
  <h1>Hello World</h1>
</section>
<section class="grad2" data-0="position:fixed;top:800px;" data-400="position:fixed;top:800px;" data-800="position:fixed;top:000px;">
  content
</section>
<section class="grad3" data-0="position:fixed;top:1200px;" data-400="position:fixed;top:1200px;" data-800="position:fixed;top:400px;">
  content
</section>

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

Set up Vue.prototype prior to the component loading

In my Vuejs code, I am utilizing the plugins feature to define a global shared variable. Vue.use(shared) The shared variable is defined as follows:- export const shared = { config: getAppConfig() } shared.install = function() { Object.definePropert ...

jQuery causing YouTube object template to not display when links are added

After storing an object template in a variable, I am using jQuery to add video links. This is being done in preparation for adding the links into the object template through ajax in the future. However, the video is not appearing as expected and I'm ...

Confirming the presence of an image using jQuery without enforcing it as mandatory

Situation: In my setup, I maintain a database that holds details about various items. Currently, I utilize a dynamic form to retrieve and exhibit the existing information on any item stored in the database. Any modifications made on the form are promptly ...

Sending Information within Controllers with AngularJS

I have a unique scenario in my application where I need to pass input from one view to another. I have set up a service as shown below: .service('greeting', function Greeting() { var greeting = this; greeting.message = 'Default&ap ...

The interaction between background-size: cover and background-position

I wanted to experiment with setting background-size: cover and testing out different background positions. Feel free to check out the live fiddle for a hands-on experience. HTML <div id="container"> </div> CSS #container { backgro ...

What could be causing my React app to consistently reload whenever I save a file in my project?

Hi there, I am currently working on a project using React, GraphQL, Node, and MongoDB. I have been trying to upload images to a folder within my app, but I am facing an issue with the app reloading after saving the file. I attempted to manage a local state ...

What is the best way to integrate TimeOut feature into an existing slider code?

I'm currently working on a simple slider with buttons that is functioning well. However, I am looking to incorporate the TimeOut() function into the existing code to enable automatic slide transitions. My attempts to achieve this using jQuery have be ...

Dealing with interstitial advertisements on mobile devices: What is the best approach for handling zoom?

I am facing a challenge with displaying interstitial ads on mobile sites. Different publishers may have varying viewport settings and initial scales on their mobile sites, making it difficult to ensure that the ad appears correctly on all devices with the ...

Is it possible to implement pagination using 'useSWR' in combination with the contentful-client?

I am currently working on implementing pagination in a Next.js project using the useSWR hook. My approach seems to be functioning correctly, but I have a concern about caching due to the key parameter not being a unique string as recommended in the documen ...

Is it possible for Cypress to execute test files that are imported from outside of the Cypress folder

Currently, I am developing an E2E test framework using Cypress and encountered an issue while trying to import spec files from locations outside the traditional Cypress directory structure (which includes directories for fixtures, integration, plugins, and ...

Parsing error: 'Unexpected token' found in JSON while attempting to access an external file

Currently working on implementing backbone for a new project along with underscore, requirejs, jquery, and bootstrap. Things are progressing smoothly as I aim to include static survey question data into one of the data models. { "defaultOptions": { ...

Learn how to retrieve the TextBox value from a button click within a Nested Gridview

I am trying to extract the value of a textbox inside a Nested Gridview using jQuery in ASP.NET. When a Button within the Nested Gridview is clicked, I want to display the textbox value in an alert box. Here is an example setup: <asp:GridView ID="Grid ...

Is it possible to manipulate the content inside a div tag using JavaScript in HTML?

What is the most efficient way to dynamically adjust a data offset value within an HTML tag using JavaScript? For example: <div class="box" data-offset="2s"></div> I am looking to update the value within the data-offset attribute based on s ...

Setting up the Node.js file system for an Ionic project: A step-by-step guide

Seeking a solution to delete a Folder/File on the client side using javascript/jQuery/AngularJS1. After researching, I found that it can be done using Node.js as shown in this Sitepoint link. Now, I am unsure how to integrate Node.js fs(File System) with ...

Combining and arranging numerous items in a precise location in three.js

I am currently working on a web application using three.js with Angular and facing some challenges when trying to set the precise positions of objects. The requirement is to load various objects and position them in specific locations. In order to load di ...

Using jQuery version 1.2.6 to conceal the initial li element

Recently, I encountered an issue with creating an Accordion sidebar using jQuery 1.2.6. Unfortunately, I'm not able to access a newer version of jQuery due to restrictions in the CMS settings. The problem seems to be related to a bug that is causing t ...

Should you prioritize internationalizing your image alt attributes?

Do you localize your alt attribute in img tags for internationalization? Is it beneficial in the long run? ...

"Troubleshooting the issue of nested jQuery Ajax requests failing to execute properly

UPDATE: I'm getting an error saying that my li tag is considered an illegal token. I'm unsure how to resolve this issue as I believed I was appending it within a ul tag I'm tasked with fetching a JSON array of public Github repositories and ...

The use of special characters in jQuery for specific selectors

I'm currently attempting to implement a basic hover effect on a special character as a selector. The special symbol I am utilizing is the double-daggers ( ‡ &#8225;) character with ASCII code 8225, and I would like it to function as a toolt ...

Performing mathematical calculations using javascript

In my project, I'm utilizing HTML, CSS, and JavaScript to achieve the following: Dropdown menu for Category (Coffee Appliance) Dropdown menu for Product (Keurig Coffee Maker) Wattage: 1500kWh (auto-filled from Local Storage) Daily Energy Con ...