JavaScript function to ensure seamless loading of web pages - issue with ordering

Trying my best to explain this, so please bear with me.

On a page, I start with a heading and paragraph tag, followed by a series of images that represent my portfolio.

Each image serves as a link that, when clicked, triggers an AJAX call to dynamically load content below the image on the page.

When a user clicks on the title of an image, I aim for the following actions to take place...

  • The window scrolls to the top of that particular image
  • All borders are removed (you can refer to the JSFiddle link for a visual example)
  • Through AJAX, new content is fetched and displayed beneath the selected image

The challenge I'm facing stems from the need to clear the content above the image. When the page scrolls to the chosen portfolio piece and removes the preceding content, it leads to instability and jitteriness.

Have a look at the following link...

https://jsfiddle.net/oterdwn1/

And here's the current state of my JavaScript code...

JS

$('a.video').on('click', function(e) {

  'use strict';
  e.preventDefault();
  if ($('.hero-pane').css('overflow') == 'hidden') {
    return false;
  }

  var dstn = $(this).attr('href');
  var className = $(this).data('class');


  var tgt = $(this);


  $('.studies a').each(function() {
    $('.hero-pane').css({
      opacity: 0
    });
    if ($(this).data("class") !== className) {
      $(this).hide();
    }
  })
  $('.hero-pane').css('overflow', 'hidden');
  $('.hero-pane').css('min-height', '0px');
  $('.hero-pane').css('max-height', '1000px');


  $('html,body').animate({
      scrollTop: (tgt).offset().top
    },
    800,
    function() {

      $(tgt).find($('.border-left, .border-right')).css('width', 0);
      $(tgt).find($('.border-top, .border-bottom')).css('height', 0);

      $('.hero-pane').delay(200).animate({
        'max-height': '0px'
      }, 100);
      $('.hero-pane').delay(200).animate({
        'padding-top': '0px'
      }, 100);
      $('.hero-pane').delay(200).animate({
        'padding-bottom': '0px'
      }, 100);
      $('.studies').delay(1000).animate({
        'margin-top': '-100px'
      }, 400);
      $('html,body').delay(1000).animate({
        scrollTop: 0
      }, 400, function() {
        //$('html, body').css({
        //   overflow: 'visible',
        //   height: 'auto'
        //});
      });


    });


  $('#' + className + '-study-title').delay(1500).fadeOut(1000, function() {
    getPageHTML(dstn, className);
  });


});

Answer №1

It appears that the styling for max-height cannot be animated directly. Consider using the "slideUp" method instead.

$('a.video').on('click', function(e) {

  'use strict';
  e.preventDefault();
  if ($('.hero-pane').css('overflow') == 'hidden') {
    return false;
  }

  var dstn = $(this).attr('href');
  var className = $(this).data('class');


  var tgt = $(this);


  $('.studies a').each(function() {
    $('.hero-pane').css({
      opacity: 0
    });
    if ($(this).data("class") !== className) {
      $(this).hide();
    }
  })
  $('.hero-pane').css('overflow', 'hidden');
  $('.hero-pane').css('min-height', '0px');
  $('.hero-pane').css('max-height', '1000px');


  $('html,body').animate({
      scrollTop: (tgt).offset().top
    },
    800,
    function() {
      $(tgt).find($('.border-left, .border-right')).css('width', 0);
      $(tgt).find($('.border-top, .border-bottom')).css('height', 0);
      $('.hero-pane').slideUp(1000)
      $('.hero-pane').delay(200).animate({
      //  'max-height': '0px',
        'padding-top': '0px',
        'padding-bottom': '0px'
      }, 100);
      return;
      $('.studies').delay(1000).animate({
        'margin-top': '-100px'
      }, 400);
      $('html,body').delay(1000).animate({
        scrollTop: 0
      }, 400, function() {});

    });

  $('#' + className + '-study-title').delay(1500).fadeOut(1000, function() {
    getPageHTML(dstn, className);
  });


});
.studies {
  position: relative;
}

.video {
  height: 100vh;
  min-height: 650px;
  border: 0;
  display: block;
  height: auto;
  background: #fff;
  padding-bottom: 0;
  border-color: white;
  overflow: hidden;
  border-style: solid;
  position: relative;
}

.video img {
  border-right: 1px solid #fff;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  min-width: 100%;
  min-height: 100%;
  width: auto;
  height: auto;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.border-top,
.border-bottom,
.border-top:after,
.border-bottom:after,
.border-left,
.border-right,
.border-left:after,
.border-right:after {
  position: absolute;
  z-index: 9;
  background-color: #fff;
}

.border-bottom {
  bottom: 0;
  top: auto;
}

.border-top,
.border-bottom {
  width: 100%;
  height: 65px;
  transition: all 1s;
  top: 0;
}

.border-left,
.border-right {
  height: 100%;
  left: 0;
  width: 65px;
  transition: all 1s;
  bottom: 0;
}

.border-right {
  right: 0;
  left: auto;
}

.border-bottom {
  bottom: 0;
  top: auto;
}

.brand-title {
  position: absolute;
  width: auto;
  bottom: 7.5%;
  left: 7.5%;
  padding: 15px;
  text-align: left;
  z-index: 1000000;
}

html {
  height: 100%;
}

body {
  height: 100%;
  overflow: auto;
}

body::-webkit-scrollbar {
  display: none;
}

::-webkit-scrollbar {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="page-wrapper">
  <div class="hero-pane">
    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-12 col-md-8">
          <h1>Title</h1>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
            irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
            irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis no...
        </div>
      </div>
    </div>

  </div>
  <div class="studies">

    <a href="#" data-class="brand1" class="video brand1" style="z-index: 2">
      <div class="border-top"></div>
      <div class="border-bottom"></div>
      <div class="border-left"></div>
      <div class="border-right"></div>
      <div id="brand-study-title" class="brand-title">
        <div class="faux-link">
          <h4>Title</h4>
        </div>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing eli.</p>
      </div>
      <img src="http://lorempixel.com/400/200/sports/">
    </a>

    <a href="#" data-class="brand2" class="video brand2" style="z-index: 2">
      <div class="border-top"></div>
      <div class="border-bottom"></div>
      <div class="border-left"></div>
      <div class="border-right"></div>
      <div id="brand-2-study-title" class="brand-title">
        <div class="faux-link">
          <h4>Title 2</h4>
        </div>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing eli.</p>
      </div>
      <img src="http://lorempixel.com/400/200/sports/">
    </a>

  </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

Suggestions for relocating this function call from my HTML code

I have been working on updating a javascript function that currently uses an inline event handler to a more modern approach. My goal is to eliminate the unattractive event handler from the actual HTML code and instead place it in a separate modular javascr ...

"Exploring the intricacies of API request methods in the ME

Currently, I am engrossed in Simon Holmes' book titled 'Getting MEAN with mongo, express, angular and node'. It's a fantastic read so far, but one major question has been nagging at me which the book doesn't seem to address. Holmes ...

Server response not being awaited by Ajax call

Currently running WAMP v.2.5 on a Windows10 system for my PHP project connected to a MySQL DB that involves multiple AJAX calls, all functioning correctly. However, there is one specific call that keeps throwing an 'Unexpected end of input' error ...

Invalid JSON syntax: found an unexpected token, the letter

JSON.parse() doesn't seem to be functioning properly within my code, and I'm unsure of the root cause. JavaScript Code: var obj = JSON.parse(this.responseText); console.log(obj.status + " " + obj.type); if (obj.status == "success") { documen ...

How to effectively pass data between parent and child controllers in Angular 1 - Seeking guidance

Currently, I am working on two separate applications that have a common requirement of displaying active incidents and closed incidents. Both apps involve similar logic for creating, modifying, saving, and deleting incidents. I am exploring the best appro ...

Why am I receiving a 404 error when trying to access an Angular 9 route within a subfolder

I am facing an issue with my Angular 9 app that is hosted in a domain subfolder. The problem lies in the index.html base tag, shown below: <base href="/subfolder/"> Everything works perfectly fine when the app is run locally without the ba ...

Discovering the method for retrieving JavaScript output in Selenium

Whenever I need to run JavaScript code, the following script has been proven to work effectively: from selenium import webdriver driver=webdriver.Firefox() driver.get("https:example.com") driver.execute_script('isLogin()') However, when I atte ...

Streamlining the process of implementing click events on elements selected by class using jQuery

Slowly but surely, I am gaining familiarity with jQuery and have reached the point where I desire to abstract my code. However, I am encountering issues when attempting to define click events during page load. Within the provided code snippet, my objectiv ...

Error encountered in VUE JS 3: The function vue.initDirectivesForSSR is not recognized as a valid function

I've been encountering some errors while trying to build my Vue web app with this configuration. I have searched for a solution, but couldn't find anyone facing the same issue as me. Any suggestions on how to resolve this problem? The build was s ...

Guide on comparing a string value with a date in the format of YYYY-MM-DD using protractor in a node js environment

In my application, I am retrieving the date field in string format using the getText() function. I need assistance in determining whether the captured date is earlier than the current date. Can someone provide guidance on this? var actDate = locator.getTex ...

Strange behavior of top navigation bar with fixed positioning in Firefox

I am currently working on a project that utilizes the Simple Sidebar template from startbootstrap.com. Our navigation bar has a position:fixed and a search box floated to the right. However, in Firefox, 50% of the search box extends outside of the enclosin ...

Is there a way to determine if the text has been shortened after pasting it into an input box that has a maxlength restriction set?

When it comes to HTML input elements, there is a maxlength attribute that limits the length of text that can be inputted. If a text longer than the specified maxlength is pasted into the input box, it will be automatically truncated to fit within the limit ...

Use JQuery version 1.8 or above to link the mousewheel

Currently, my website is utilizing Jquery 1.8.3. Unfortunately, I am facing an issue where I am unable to bind the mousewheel with this version of jQuery, while the 1.4.2 version successfully binds the mousewheel. I am seeking a solution to overcome this ...

iOS Safari is displaying the keyboard with a comma instead of a period

Is there a way to display a comma instead of a period on the iOS keyboard in Safari? Upon tapping on the input field, users are presented with a keyboard featuring a period. https://i.sstatic.net/nMFCg.png ...

Achieve a form submission redirection using ajax

I am looking to customize the form response process by sending it to a Google sheet and then redirecting the user to a customized thank you page instead of the default report page. Form: <form method="post" id="formID" action="gscript"> ... </fo ...

Using optional URL parameters in Express.js

I'm in the process of creating a basic app using expressJS to retrieve user data (similar to an API). I currently have a /users route set up to fetch all users, which is functioning correctly. app.get('/users', (req: express.Request, res: ex ...

What is the best way to include a .AVI file in an HTML document?

I have come across some examples of .AVI in HTML on the internet. However, my page seems to be having issues. It displays fine in Chrome on my computer. But in Internet Explorer, there is a bar at the top and the videos appear blank afterwards. It ...

The concept of an HTML pop-up message that hovers above the content

While working on my HTML form, I encountered an issue regarding the display of a warning message notifying users about the caps lock being on. Currently, the warning is shown correctly in a div located to the right of the text box. However, this div's ...

Issue with VueJS rendering data within a for loop

As a newcomer to VueJS, I appreciate your patience as I navigate through this. Let me provide as much detail as possible. I am currently working on a Vue app that needs to retrieve a response from a server, iterate through the data, and set a Vue data var ...

HTML code containing a dropdown menu with selectable options pulled from a Django framework

Here is the HTML code snippet I am working with: <select name="txn" tabindex="3" id="txn" class="fixed" onfocus="highlightInput(this.id)" onblur="unhighlightInput(this.id)"> <option value="RR" my="{{txn|genselected:"RR"|safe}>RR</option ...