Removing Inline Styles Using jQuery Scroll Event

Whenever I reach the scroll position of 1400 and then scroll back to the top, my chat elements (chat1, chat2, chat3, chat4) receive an "empty" inline style but only for a duration of 1 second. After that, the fadeIn effect occurs again every time I scroll with a 1-second delay.

Essentially, I aim to display four images consecutively when scrolled to 1400 and make them disappear when scrolling back to the top :)

(window).scroll(function() {
  var scrollPosition = $(window).scrollTop();
  
  var phoneScreen = $('.phone-screen');

  var chat1 = phoneScreen.find('#chat1');
  var chat2 = phoneScreen.find('#chat2');
  var chat3 = phoneScreen.find('#chat3');
  var chat4 = phoneScreen.find('#chat4');
  

  if(scrollPosition >= 1400){
    chat1.delay(1000).fadeIn(200, function(){
      chat2.delay(200).fadeIn(200, function(){
        chat3.delay(200).fadeIn(200, function(){
          chat4.delay(200).fadeIn(200);
        });
      });
    });

  }else{
    chat1.removeAttr('style');
    chat2.css('display', '');
    chat3.css('display', '');
    chat4.css('display', '');
  }
});

This is my CSS:

#chat1{
  position: relative;
  top: -1356px;
  z-index: 16;
  display: none;
}

This is my HTML:

<div class="col-md-4">
  <div class="phone-container">
    <img src="images/Tuto/phone-frame.png" alt="Phone frame">
    <div class="phone-screen">

    <img src="images/Tuto/phone-background.png" alt="Phone background" id="phone-bg">
    <img src="images/Tuto/phone-footer-explore.png" alt="phone footer explore" id="phone-fot-exp">
      <div class="profile-screen">
        <img src="images/Tuto/Joanna.png" alt="Girl Profile" id="Girl-one-prof">
        <img src="images/Tuto/szymon.png" alt="Girl Profile" id="boy-one-prof">
      </div>
    <img src="images/Tuto/match.png" alt="match" class="match">
    <img src="images/Tuto/chat.png" alt="chat" id="chat">
    <img src="images/Tuto/chat1.png" alt="chat1" id="chat1">
    <img src="images/Tuto/chat2.png" alt="chat2" id="chat2">
    <img src="images/Tuto/chat3.png" alt="chat3" id="chat3">
    <img src="images/Tuto/chat4.png" alt="chat4" id="chat4">
  </div>
</div>

Answer №1

Would you consider utilizing fixed-positioned elements for this task?

Furthermore, each time you scroll up, an animation is triggered on every img. These animations take a while to start and complete, causing .hide() to struggle in attempting to hide an object affected by an ongoing animation. This is where .stop(true) comes in to the rescue and, as stated at this source, it "Stops the currently-running animation on the matched elements."

You have the option to refactor this code so that it only triggers the animations ONCE when it surpasses the 1400px mark, and AGAIN only when it surpasses it upwards.

$('.phone-screen').children('img').each(function(i){
  $(this).css('top', i * $(this).prev().height());
});
$(window).scroll(function() {

var scroll1 = $(window).scrollTop();

var phoneScreen = $('.phone-screen');

var chat1 = phoneScreen.find('#chat1');
var chat2 = phoneScreen.find('#chat2');
var chat3 = phoneScreen.find('#chat3');
var chat4 = phoneScreen.find('#chat4');

//==================== THIS PART =======================//
if(scroll1 >= 1400){
    chat1.delay(1000).fadeIn(200, function(){
        chat2.delay(200).fadeIn(200, function(){
            chat3.delay(200).fadeIn(200, function(){
                chat4.delay(200).fadeIn(200);
            });
        });
    });

}else{
  $('[id^="chat"]').stop(true).hide();
}
});
[id^="chat"]{
    position: fixed;
    top: 0;
    z-index: 16;
    display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="phone-screen">
  <img id="chat1" alt="chat1">
  <img id="chat2" alt="chat2">
  <img id="chat3" alt="chat3">
  <img id="chat4" alt="chat4">
  <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
</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

Is there a maximum file size limit for uploads in jQuery when using ajax requests?

I'm curious if there's a size limit for jQuery uploading (via Ajax)? Specifically, I'm working on a form to upload multiple images to the server (currently using WampServer locally). So far, I've been able to easily upload one, two, or ...

Obtaining the XML element containing a designated attribute name

Greetings and thank you in advance, I have a question regarding the AJAX return and parsing of XML information. While I am successfully returning a request, I am fetching all the XML nodes with the tag ngPropertyName. However, I wish to retrieve only the ...

How can I emphasize the React Material UI TextField "Cell" within a React Material UI Table?

Currently, I am working on a project using React Material UI along with TypeScript. In one part of the application, there is a Material UI Table that includes a column of Material TextFields in each row. The goal is to highlight the entire table cell when ...

I am concerned about the security of my games as they can be easily hacked through right-click inspect. What measures can

As a newcomer to game development, I am creating web games using JS, HTML, and CSS. However, I have encountered an issue with preventing right-click inspect hacking, which has led to people hacking my games through this method. I am wondering, how can I s ...

Tips for implementing `overflow-x: hidden` on a `ValueContainer` when using `isMulti` in react-select V2

I'm currently grappling with finding a solution to have a react-select V2 component, configured as isMulti, hide selected values that exceed the width of the ValueContainer rather than breaking to a new line and expanding the component's height. ...

Representation of a family tree in CSS showcasing multiple sub-parents (both many to one and one to many relationships)

I've come across various family tree presentations. I'm curious if it's possible to display a one-to-many and then many-to-one structure? Currently, the flow is linear stop after stop, but I'd like to incorporate multiple steps that con ...

Vuetify's offset feature seems to be malfunctioning as it does not

I'm facing an issue with the <v-flex> element in my code, specifically with the offset property not responding as expected. Despite following the recommended layout from the vuetify docs, I can't seem to get it right. Below you can see the ...

Instead of uploading multiple files at once, allow users to upload individual files by clicking on the table row

In my dynamic table, there are 4 columns. The first 3 columns in each row have an input type='file' where users can choose a file to upload, and the 4th column has a submit button aligned with the rows. While submitting files in the first row wor ...

How can I add text to a textbox within a duplicated div using Javascript or Jquery?

I'm looking to add text to a cloned div's text box using jQuery. I have a div with a button and text box, and by cloning it, I want to dynamically insert text into the new div. $(document).ready(function () { $("#click").click(function () { ...

Steps to send a table to PHP and store it as a text file

I have this program for a form data entry. It includes text boxes to input values, and upon clicking 'Submit', the input values should be displayed below while resetting the text box for another set of inputs. The issue I am facing is with the sa ...

Creating trails by following the cursor's movement in KineticJS

Currently, I am working on a drawing application using KineticJS. While I have successfully used it to draw shapes and straight lines by following the method outlined in KineticJS - Drawing Lines with Mouse, I now need to draw a line along the mouse's ...

Personalize the error message for throwing an exception in JavaScript

I've been working on customizing error messages for exceptions thrown in JavaScript. Despite my best efforts, I have not been successful so far. I'm currently attempting the following code snippet but it's not functioning as expected: f ...

Display a particular div when a specific image is present within another div

I'm currently working on a task, but I'm having trouble with it. If I have an image called smiley.png within a div with the class "selected." <div class="selected" style=""><img width="25" height="24" src="images/smileys/smiley.png ...

Vertical alignment of content over image is not in sync

I am attempting to center my div container .home-img-text vertically in the middle of its parent div .home-img. Despite trying various methods such as setting .home-img-text to position: absolute, relative, adding padding-top, and several others, I haven&a ...

Using jQuery to determine if the child element is a <ul> tag

HTML: <ul class="menu"> <li><a href="http://example.com">Text</a> <ul> <li><a href="http://example.com">Text</a> <li><a href="#">Text</a> <li><a href="# ...

What is the best way to utilize *ngFor for looping in Angular 6 in order to display three images simultaneously?

I have successfully added 3 images on a single slide. How can I implement *ngFor to dynamically load data? <carousel> <slide> <img src="../../assets/wts1.png" alt="first slide" style="display: inline-blo ...

Utilizing HTML5 audio elements with jQuery enhances the auditory experience

As I delve into creating a simple media section for a website, I have encountered some challenges that I would like to address. Let me explain the issue I am facing. I currently have a 'play list' consisting of 'links' - represented by ...

The method of utilizing React with Redux to display component properties

I am currently trying to include my common component in my main.js file Successfully implemented this However, when attempting to print my Redux data values in the common component, I created a method called handleClickForRedux to handle this task. Even af ...

Enhance JQuery functionality using Typescript

Currently, I am in the process of developing a Typescript plugin that generates a DOM for Header and attaches it to the page. This particular project utilizes JQuery for handling DOM operations. To customize the plugin further, I aim to transmit config Opt ...

After the URL of the window was updated, the local storage was automatically cleared

function makeAjaxCall(){ ............... ............... success:function(response){ localStorage.setItem("token", response['token']) window.location.href="https://www.example.com/profiler/" } } The code snippet above is fo ...