What is the method for creating a div that scrolls only partially?

Google has impressed me with their work:

https://developers.google.com/kml/articles/raster

I am curious about creating a design similar to the one on top of the page, where there is a fixed bar and a scrolling "light aqua" div that stops just below the "dark aqua". Can this be achieved using only CSS?

Answer №1

To achieve this functionality, you can utilize jQuery. Look up the offset().top and scrollTop() functions within jQuery. The basic idea is to adjust the element's position property to fixed when the space between the element and the top of the document diminishes and reaches zero.

$(document).ready(function() 
{
  window.holdOffset = $('#sticky_navigation').offset().top;
  $(window).scroll(function() 
  {
    sticky_navigation();
  });
});

function sticky_navigation()
{
  var scroll_top = $(window).scrollTop(); 
  if (scroll_top > holdOffset)  
    $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 }); 
  else 
    $('#sticky_navigation').css({ 'position': 'relative' });    
};
body
{
  margin:0px;
}

p
{
  margin-left:100px;
  margin-right:100px;
}

#my_logo 
{
  font-size:80px;
}


#sticky_navigation_wrapper 
{
  width:100%;
  height:50px; 
}

#sticky_navigation 
{
  width:100%; 
  height:50px; 
  background:black; 
}

#sticky_navigation ul 
{
  list-style:none; 
  margin:0; 
  padding:5px; 
}


#sticky_navigation ul li a 
{
  display:block; 
  float:left; 
  margin:0 0 0 5px; 
  padding:0 20px; 
  height:40px; 
  font-size:24px; 
  color:#ddd; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title></title>

<link href="demo.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="sticky-navigation.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>


</head>
<body>

<div id="demo_top_wrapper">

<div id="demo_top">
<div class="demo_container">
<div id="my_logo">Site Logo</div>
</div>
</div>

<div id="sticky_navigation_wrapper">
<div id="sticky_navigation">
<div class="demo_container">
<ul>
<li><a href="#" class="selected">Link #1</a></li>
<li><a href="#">Link #2</a></li>
<li><a href="#">Link #3</a></li>
</ul>
</div>
</div>
</div>


</div>

<section id="main">

<div id="content">

<p><strong>Scroll down to see this navigation menu sticking to the top of the page.</strong></p>

...[content continues here]...

</div>

</section>

<script type="text/javascript" src="myscript.js"></script>
</body>
</html>

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

Issue with grunt-closure-tools: ERROR - There is a parse error due to using a reserved word as an

Currently, I am utilizing the "grunt-closure-tools" tool. Initially, minifying a simple JS file was successful without any issues. However, encountering an exception arose when attempting to minify either the AngularJS or Bootstrap libraries: Error: Comm ...

Finding out the specific row that was clicked in a Jquery Mobile listview

I've searched everywhere and can't find a solution. How can I retrieve the value of a row tapped in a listview? This could be anything from the name to the index within the object. Currently, I have a function handling the tap event. I need to pa ...

Error encountered while using the getJSON code syntax

Is there an issue with this code snippet? I am receiving a syntax error on line 1: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Demo</title> </head> <body> <script src="http:/ ...

Transmit information via AJAX using the React Flux design pattern

Here is my React code snippet: var AddRecord = React.createClass({ getInitialState: function() { return { Data: [] } }, sendData : ...

Communication between child and parent components in Vue.js is an essential feature

Attempting to invoke functions from my component to Vue for the login process. This is the code snippet of my component : Vue.component('auths', { data: function() { return { ip: '', sessiontoken: '' } ...

Is there a way to merge all this data into a single Object?

This particular situation is quite complex. Let's consider the following scenario: I have two JavaScript objects that are fetched via REST calls, using two different callbacks. So, we have: call1() - POST method - parsed JSON to JavaScript object, ...

What is the best way to display the entire background image in a jumbotron?

After some experimentation, I discovered that by increasing the amount of content I have, it displays the full background image. Is there a clean and efficient way to achieve this? <div class="jumbotron jumbotron-fluid" style="background: url('./a ...

Navigating with AngularJS through link redirection using anchor tags

How can I achieve smooth scrolling to a specific section using angularJS when clicking on a link? When clicking on a link like this: The page instantly redirects to the specified footer section. I want to implement an angular controller to handle this fun ...

"Unlocking the Power of Colormap in JavaScript: A Comprehensive

I am in need of incorporating a colormap into my JavaScript page. My server side is powered by Flask, written in Python. To render colormaps on the client side, I have a JavaScript file that requires the colormap module (installed using "npm install colorm ...

Implementing an input field using JavaScript

I have multiple search forms, each containing an input field with the class "search_input". My goal is to add the following code on button click: <input type="submit" class="searchbtn" value="Search" /> next to each input <input type="text" cl ...

The error property is not found in the type AxiosResponse<any, any> or { error: AxiosError<unknown, any>; }

As a beginner with typescript, I am encountering some issues with the following code snippet import axios, { AxiosResponse, AxiosError } from 'axios'; const get = async () => { const url = 'https://example.com'; const reques ...

Showing JSON information in an Angular application

Upon page load, I am encountering an issue with retrieving and storing JSON data objects in Angular scope for use on my page and in my controller. When attempting to access the value, I receive the error message: angular.js:11655 ReferenceError: data is ...

.eslintignore not functioning as intended

Recently, I started working on a Vue template project that includes ESLint. However, I found that I wanted to disable ESLint for some reason. To do this, I followed the recommended steps and created a file with the following content: **/*.js This file wa ...

JavaScript Database Operations

I have a script that reads data from a Google Sheet and returns its content. I am looking to save all of this data into a database. var request = gapi.client.sheets.spreadsheets.values.get(params); request.then(function(response) { console.log(respons ...

Uploading files along with additional data fields

I am facing a dilemma with my HTML form that has enctype="multipart/form-data" attribute. I have a DTO class with all the necessary setters and getters. Since I am submitting the form as multipart, I cannot use getParameter() method directly. In my servlet ...

Using AXIOS and VueJs to visually represent data with a two-dimensional array

I'm new to vuejs and I want to display my data response in a two-dimensional table. This is the data I have: [{ "origine": "", "nb": 15 }, { "origine": "?", "nb": 18 }, { "origine": "?L", "nb": 1 }, { "origine": "G", "nb": 298 }, { ...

JavaScript regular expression pattern matching

After encountering a peculiar URL, I found myself in this dilemma: var oURL = "https://graph.facebook.com/#{username}/posts?access_token=#{token}"; I am determined to extract both the username and token from it; My first attempt was: var match = (/#&bs ...

Create a replica of a Facebook share pop-up widget

I attempted to replicate the share button found at the bottom of this unsettling website: WARNING! THE CONTENT ON THIS SITE MAY BE DISTURBING!!! When I click on it, the link leads me here: If I copy the code: <a href="https://www.facebook.com/dialog ...

Tips for modifying the text color of radio button choices in HTML and CSS

I have set a black background image for my HTML page. How can I change the radio button labels/texts to white, similar to the questions? This is the code snippet that shows how it currently looks on the page: View Image <hr&g ...

Error encountered in loop for concatenating html elements: identifier unexpectedly found (jquery + html + php)

I am attempting to concatenate HTML inside a variable within a loop and then return it populated. However, I am encountering an error: Uncaught SyntaxError: Unexpected token += in my code. <a style="cursor: pointer" id="addmore">More Entree ?</ ...