Move images horizontally next to the height of an element

I am attempting to increase the top attribute in relation to the element it is currently adjacent to. The element should only scroll directly next to the other element and cease when it surpasses the height of that element.

My Goal: I want the image to remain fixed beside the element on the right using jQuery, stopping at the end of the element.

var objectSelect = $(".article");
var objectPosition = objectSelect.offset().top;

//if (scroll > objectPosition) {
//  objectSelect.find('article').addClass("fixable").find('figure').addClass("fixable");
//} else {
//  objectSelect.find('article').removeClass("fixable").find('figure').removeClass("fixable");
//}
body{
  
  height:1000px;
  
  }

.article {
  width: 100%;
  display: block;
}
.wrapper {
  position: relative;
}
figure {
  position: absolute;
  left: 0;
  top: 0;
  width: 50%;
  padding: 0;
  margin: 0;
  overflow: hidden;
}
.other-content {
  background-color: black;
  float: right;
  overflow: hidden;
  display: block;
  width: 50%;
  height: 800px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="article">
  <div class="wrapper">
    <figure>
      <img src="https://placeholdit.imgix.net/~text?txtsize=53&txt=566%C3%97780&w=566&h=500" />
    </figure>
    <div class="other-content">

    </div>
  </div>
</div>

Answer №1

To keep the figure element fixed until it reaches the bottom of .other-content, then switch to absolute positioning with bottom: 0, and revert back to fixed position when scrolling back up past the top of the figure.

var $figure = $('figure'),
  $other = $('.other-content'),
  other_bottom = $other.offset().top + $other.outerHeight();

$(window).on('scroll', function() {
  var scroll = $(window).scrollTop(),
    figure_top = $figure.offset().top,
    figure_bottom = $figure.offset().top + $figure.outerHeight();
  if (!$figure.hasClass('abs') && figure_bottom >= other_bottom) {
    $figure.addClass('abs');
  }
  if ($figure.hasClass('abs') && scroll < figure_top) {
    $figure.removeClass('abs');
  }
});
body {
  height: 500vh;
  margin: 0;
}

.article {
  width: 100%;
  display: block;
}

.wrapper {
  position: relative;
  overflow: auto;
}

figure {
  position: fixed;
  left: 0;
  top: 0;
  width: 50%;
  padding: 0;
  margin: 0;
  overflow: hidden;
}

.abs {
  position: absolute;
  bottom: 0;
  top: auto;
}

.other-content {
  background-color: black;
  float: right;
  overflow: hidden;
  display: block;
  width: 50%;
  height: 800px;
}

img {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="article">
  <div class="wrapper">
    <figure>
      <img src="https://placeholdit.imgix.net/~text?txtsize=53&txt=566%C3%97780&w=566&h=500" />
    </figure>
    <div class="other-content">
    </div>
  </div>
</div>

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

Encountering a null value in a function parameter assigned within an AJAX success callback in JavaScript

function fetchData(url) { var response = null; $.ajax({ type: "GET", url: url, crossDomain: true, async: false, contentType: "application/json; charset=utf-8", da ...

Proper method for extracting and sending the final row of information from Google Sheets

The script I have is successfully formatting and sending out the information, but there is an issue. Instead of sending only the latest data, it is sending out each row as a separate email. I specifically want only the last row of data to be sent. functio ...

If the desktop website is zoomed in beyond 150%, it will automatically switch to mobile responsive mode

Whenever the desktop website is zoomed above 150%, it automatically switches to mobile responsive mode. Is there a way to disable this feature? You can find the website in question here: Give it a try by zooming to 150 percent and see for yourself. Appr ...

Unit testing Vue 3 by simulating the push() method of vue-router

As a newcomer to Vue and StackOverflow, I wanted to share my issue. While attempting to run the unit test for my application, I encountered a TypeError. The error message stated: "TypeError: Cannot read properties of undefined (reading 'push')" ...

Is it feasible to convert a Google Drive spreadsheet into JSON format without needing the consent screen?

I'm working on incorporating a JSON feed directly from a private spreadsheet (accessible only via link) onto my website. In order to do this, I must create a new auth token using OAuth 2.0, which is not an issue. However, the Google Sheets API v4 mand ...

`switch tabs visibility based on checkbox selection`

There are 4 tabs available on a JSP page: Tab 1, Tab2, Tab3, and Tab4. Initially, only Tab1 is enabled, allowing users to freely work within that tab. However, Tabs 2, 3, and 4 are disabled, preventing users from accessing their contents. A checkbox is lo ...

Is it possible to dynamically adjust the width of table columns using CSS/LESS on a per-column basis?

In this scenario, the table's column width is determined by the number of columns present. For example, if there are 4 columns in the table, each column's width would be set to a maximum of 25% of the table's overall width. With 3 columns, t ...

List of prices with a dotted line separating the price and product, adjusting its width based on

I am attempting to create a price list with a dotted underline between the price and product that adjusts dynamically in width. Here is my code snippet: https://jsfiddle.net/romariokbn/2gm27rv6/ <ul class="how-can-i-do"> <li> <span ...

Error: JavaScript object array failing to import properly

In my code, I have an array of objects named trace which is defined as follows: export const trace: IStackTrace[] = [ { ordered_globals: ["c"], stdout: "", func_name: "<module>", stack_to_render: [], globals: { c: ["REF" ...

Are you able to locate <td>s with identical classes using just a portion of the string?

There are multiple classes in the <td>s of my table. I am looking to identify each <td> that contains a specific string. For instance: <table> <tr> <td class="hello there">foo</td> <td class=" ...

What is the best method for restricting the visibility of an animation to specific regions within an SVG?

I am seeking assistance with adding a "shine" animation to my SVG. I want the animation to only appear within specific parts of the SVG, but I'm uncertain about how to achieve this. Below is what I have accomplished so far. The aim is for the white ...

Is it possible to use a variable in JSX within a map function to modify inline CSS styles?

I have a map function that loops through an array and generates divs on the DOM for each color item. The array consists of hex values representing different colors (e.g. #1a703f). The goal is to set the background of each div to match the corresponding co ...

How can I navigate to a specific section within a Bootstrap modal without being redirected back to the origin of the modal?

Currently in the process of creating a basic front end website with Bootstrap for styling. One section I've developed includes modals, where users click a button to open a centrally located modal window. Desired Functionality I aim to enable users t ...

Creating Filled DIVs using JavaScript

Although it may appear to be a common inquiry, my specific needs have not been addressed in any of the Stack threads I've come across. I am dealing with a series of nested DIV statements, as demonstrated here (not all CSS included). I am looking to ...

Troubleshooting: Why is my express-fileupload failing to upload images

I'm currently working on implementing a feature that allows users to upload a profile image for their profiles. I have the form set up the way I want it, but I keep encountering an error that says TypeError: Cannot read property 'profilePicUpload ...

Is there a way for my React application to detect changes in an npm package?

I am currently customizing an npm package for my application, but I am facing issues with it not being detected when starting my development server. Previously, I was able to resolve this by removing the library and reinstalling it, followed by replacing t ...

Tips for customizing the appearance of a disabled checkbox

I'm curious if you have any ideas on how to customize the appearance of a disabled checkbox? For example: <input type="checkbox" value="All Terrain Vehicle" name="exfilter_All Terrain Vehicle" id="exfilter_All_Terrain_Vehicle" ...

Why does appending to a TextArea field fail in one scenario but succeed in another when using Javascript?

There is a JavaScript function that captures the value from a select dropdown and appends it to the end of a textarea field (mask) whenever a new selection is made. function addToEditMask(select, mask) { var selectedValue = document.getElementById(sel ...

Determine whether an element is currently focused using a Vue.js directive

I'm attempting to verify if an element is currently in focus, specifically an input field, and then apply a class to another element. This is the code I have been working on, but for some reason the hasFocus() function isn't functioning as expect ...

What is the actual storage mechanism for HTML5 WebStorage data?

While utilizing the HTML5 WebStorage functionality, I am aware that certain browsers such as Chrome offer developer tools for users to navigate through their WebStorage contents for debugging and troubleshooting purposes. I am curious if it is feasible to ...