Having trouble figuring out how to load images into a div based on the current page location?

Looking for a solution to my problem - I have a navigation bar with a fixed div (A) on the far right side. The nav bar remains at the top of the page. There are 6 sections in the body of the page, each being 1200px tall divs. What I want is for a different image to load into div (A) when the user scrolls down from one section to another.

I've tried numerous solutions but nothing seems to work so far. Any fresh ideas or suggestions?

Answer №1

To customize the content of each div, you can utilize the .scrollTop() function in jQuery. By retrieving the value x from .scrollTop(), you can then modify the src attribute of the div accordingly.

Experiment with different data sources within your "if statements" to alter your navigation image and adjust the .scrollTop() parameter to achieve the desired outcome.

Learn more about the .scroll() event here

Find out about the .scrollTop() method here

While unable to access platforms like jsbin at work, I have successfully tested the following code snippet elsewhere. Ensure that the page actually scrolls to witness the effect.

$(function() {
  $(document).scroll(function () {
    var top = $(window).scrollTop();
    $('#navImg').html(top);
    if (top > 100) {
       $('#botImg').attr('src', 'http://animalia-life.com/data_images/dog/dog1.jpg');
    }
    if (top < 100) {
       $('#botImg').attr('src', 'http://davidfeldmanshow.com/wp-content/uploads/2014/01/dogs-wallpaper.jpg');
    }
  })
});
#nav {
  background: black;
  height: 100px;
  width:100%;
}

#navImg {
  height:30px;
  width: 30px;
  top:15px;
  left:15px;
  position:fixed;
  background:green;
}

#bottom {
  position:absolute;
  top:200px;
  height:1200px;
  width:200px;
  background:blue;
}

#botImg {
  height: 100px;
  width:100px;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id='nav'><div id='navImg'></div></div>

<div id='bottom'><img id='botImg' src='http://davidfeldmanshow.com/wp-content/uploads/2014/01/dogs-wallpaper.jpg' /></div>

Answer №2

After some searching, I finally stumbled upon the solution I needed. It's funny how such a simple thing like fixing the file path can make all the difference. Oh well, at least it's sorted now!

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

Using Ajax script to transfer user information to a php file in order to refresh the modified row in a table

Recently, I created a table in PHP to display certain data. Here's how it looks: <?php echo '<table id="tableteste" class="table table-striped" width="100%">'; echo '<thead><tr>'; echo &apos ...

What is the process for setting up custom HTML tags in Resharper?

I am looking to customize my HTML files by using custom tags to incorporate knockout components [1]: <like-widget params="value: userRating"></like-widget> To enable the tag in VisualStudio's HTML formatting settings, I made the followin ...

Arrange TD's from various TR's underneath each other in a stack

Imagine we have a table that needs to be made responsive. <table> <tr> <td>Dog Image</td> <td>Cat Image</td> </tr> <tr> <td>Dog Text</td> <td>Cat Text</td> & ...

Unable to implement a transition to adjust the scaling transformation

As I work on developing a website for a local business, my progress involves creating a menu bar. However, I am facing an issue with the transition I have applied using transform: scale(1.2) which does not seem to be working as intended. After spending h ...

Creating a dynamic table displaying data fetched from a MySQL database, with the added functionality of seamlessly filtering the table content without the need to refresh the

Currently, I am developing a website that showcases leaderboard data retrieved from a MySQL database in the form of a table. The users have the ability to filter and search through this data. By utilizing PHP calls, I was successful in constructing the tab ...

Executing various database inserts using Ajax with a changing quantity of input fields

I am currently working on adding multiple lines of text fields to my database. <html> <head> </head> <body> <table> <tr> <td><input type="text" id="n ...

The width of the HTML page seems to adjust or shift when I click on

I'm encountering an issue with my navbar. Whenever I open the dropdown menu, the width of my page changes but returns to normal when it is closed. <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="styl ...

Determine how the scale of a transform changes over time by calculating the function of elapsed time [ transform: scale(x, y

I am currently working on a container that can be expanded and collapsed simply by clicking on a chevron icon. The functionality to collapse or expand the container resides within a function called transformAnimation. This function code closely resembles t ...

Implementing Dropzone in an Angular MVC5 project

For more information about dropzone, visit this link I am currently implementing dropzone as my file upload handler with the combination of angularjs and MVC5. The challenge I'm facing is getting the Http post request to go from the angularjs servic ...

Transforming a JavaScript object into a different shape

I need help converting a list of team objects containing team names, reporters, and statuses for each day into date-based objects with all teams and their respective statuses for each date. I attempted the following code snippet but did not achieve the de ...

Implementing automatic token refreshing and automatic logout features in Vue

I am a novice web developer looking to enhance my skills. For my initial project, I decided to incorporate Laravel and Vue. My main objectives are to: Implement an auto-logout feature after 3 minutes of user inactivity Create an automatic ping to my token ...

Don't conceal the :after and :before elements when using CSS custom checkboxes

I have created my own custom checkboxes using this CSS code: input[type="checkbox"]{ display: none; } .checkbox{ position: relative; width: 60px; height: 19px; background-color: green; border-radius: 10px; } .checkbox input[type="checkbox"] + ...

The try/catch block proves ineffective at handling a socket connection exception

I am attempting to test connection to a non-existent socket. In this scenario, an exception is thrown and I anticipate it being caught in the try/catch block below. The function createConnection is imported from the net package. try { createConnection( ...

Attaching a buoyant div to the precise location of a different element

I have a unordered list (ul) with individual list items (li) that are displayed within a scrollable container. This means that only 8 list items are visible at a time, but you can scroll through them to see the others. Each list item (li) has an "edit" b ...

Ways to center the percentage on the progress bar

I'm having an issue with positioning the percentage 100% in the center of the element. I attempted adjusting the spacing in the JavaScript code, but so far it hasn't been successful. Here is the current output for the code: http://jsfiddle.net/G ...

ng-required is ineffective when used with number inputs that have a minimum value requirement

In my form, I have implemented a checkbox that, when checked, toggles the visibility of a div using AngularJS's ng-show. Within this div, there is an input field of type "number" with a validation setting of min="10000". I am trying to prevent the f ...

Navigational dropdown menu in Bootstrap

I'm a bit rusty with Bootstrap and I'm struggling to make my NavBar dropdown toggle. I've gone through all the documentation and forums available but can't seem to figure it out. Any ideas on what I might be missing? <nav class=" ...

Setting variables in AngularJS services without encountering JavaScript undefined errors

After developing an AngularJS module, I attempted to link a service and use it to share variables across the application. While most things are functioning properly, I encountered an issue when trying to set submenu[1] to an object. The error message sta ...

Interactive button visual effect

I have a piece of code that currently plays audio when I click on an image. However, I am looking to modify it so that not only does clicking on the image play the audio, but it also changes the image to another one. Can anyone assist me with this? Here i ...

Converting HTML and CSS into a PDF using JavaScript

I am on the lookout for libraries that can cater to my specific needs, as I haven't found one that fits perfectly yet. My setup involves Node.js on Express.js for the backend, and html/css/js for the front-end. Browser support includes IE8 and up, chr ...