"Experience the Dynamic Animation of Bootstrap4 with jQuery in a

I had implemented a code for horizontal page left to right animation which was working perfectly fine. However, it suddenly stopped functioning without any recent changes being made.

Now, the Page--3 link is no longer functional and clicking on either the Page--4 or Page--5 links redirects to Page--1 or Page--2.

The Page--1 link from the navbar works properly, as does the Page--2 link.

Though the Page--3 link from the navbar doesn't seem to open anything at all.

Clicking on the Page--4 link opens either page--3 or Page--2, while the behavior of the Page--5 link seems unpredictable as it sometimes works and other times leads to Page--1.

Answer №1

It seems that setting overflow to hidden eliminates the horizontal scrollbar and consequently any reliable value for scrollLeft.

My recommendation is to consider animating the margin of the parent container instead.

<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>


  <!-- Bootstrap  CSS -->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

  <style type="text/css">
    html,
    body {
      height: 100%;
      /* background: linear-gradient(to left, #283048, #859398);*/
      overflow: hidden;
    }
    
    .main {
      margin-top: 5%;
      margin-bottom: 0;
      margin-right: 0;
      mar`enter code here`gin-left: 0;
      width: 500%;
      height: 90%;
      /* Mandatory for horizontal scroll */
      overflow: hidden;
      display: inline-flex;
      white-space: nowrap;
      /* Mandatory for horizontal scroll */
    }
    
    .main .card {
      display: inline-block;
      background: transparent;
      height: 100%;
      width: 100%;
    }
    
    .c-toggler {
      border-color: white;
    }
    
    .navbar-dark .ham-icon {
      background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 30 30' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255,255,255, 1)'  stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 7h22M4 15h22M4 23h22'/%3E%3C/svg%3E");
    }
  </style>

</head>


<body>
  <div class="container container-navbar">
    <nav class="navbar navbar-expand-lg fixed-top navbar-dark bg-dark">
      <a class="navbar-brand" href="#">NavBar</a>
      <!--Brand icon here -->
      <button class="navbar-toggler c-toggler" type="button" data-toggle="collapse" data-target="#navbar-div" aria-controls="navbar-div" aria-expanded="false" aria-label="Toggle navigation">
            <span  class="navbar-toggler-icon ham-icon"></span>
          </button>

      <div class="collapse navbar-collapse navbar-div" id="navbar-div">
        <ul class="navbar-nav mr-3">
          <li class="nav-item active">
            <a class="nav-link" href="#page1" data-toggle="collapse" data-target=".navbar-collapse.show">Page--1 <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item active">
            <a class="nav-link" href="#page2" data-toggle="collapse" data-target=".navbar-collapse.show">Page--2</a>
          </li>
          <li class="nav-item active">
            <a class="nav-link" href="#page3" data-toggle="collapse" data-target=".navbar-collapse.show">Page--3</a>
          </li>
          <li class="nav-item active">
            <a class="nav-link" href="#page4" data-toggle="collapse" data-target=".navbar-collapse.show">Page--4</a>
          </li>
          <li class="nav-item active">
            <a class="nav-link" href="#page5" data-toggle="collapse" data-target=".navbar-collapse.show">Page--5</a>
          </li>
        </ul>
      </div>
    </nav>
  </div>

  <div class="container-fluid main">
    <div class="card page1" id="page1">
      <h2>Page1</h2>
    </div>
    <div class="card page2" id="page2">
      <h2>Page2</h2>
    </div>
    <div class="card page3" id="page3">
      <h2>Page3</h2>
    </div>
    <div class="card page4" id="page4">
      <h2>Page4</h2>
    </div>
    <div class="card page5" id="page5">
      <h2>Page5</h2>
    </div>
  </div>

  <!-- Bootstrap  and jQuery Scripts-->
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

  <script>
    $(document).ready(function() {
      $("body").css("overflow", "hidden");
      $('a[href^="#page"]').on('click', function() {
        event.preventDefault();
        // Horizontal Scroll
        let currentMargin = $('.main').css('margin-left').replace('px', '');
        let targetOffset = $($(this).attr('href')).offset().left;
        $('.main').animate({
          marginLeft: currentMargin - targetOffset
        }, 1000);
      });
    });
  </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

Click to load comments dynamically using Ajax

Could really use some assistance with this. I have a database containing fields like "ID | PLZ | Country | Author | Comment". Using JQuery, I was able to successfully show/hide the "Comment" field. Now, my goal is to load the comments using Ajax when the ...

Is it viable to modify the appearance of the elements in a webview?

I need to display a webview in my electron app and modify the appearance of its content. I am open to using JS or CSS to either hide elements with .hide() or visibility: none inside the webview. Can this be done? I have been having trouble finding an answ ...

Customize Highcharts axis properties after adding additional series to the plot

When working with highcharts, I utilize the xAxys and yAxis properties to format the inserted data. The code used is the same for both axes, so I'll provide an example for one of them: $('#containers').highcharts({ .... .... yAxis: { ...

Incorporating JQuery UI sortable functionality with the CSS grid display property to create a

I'm currently working on creating a sortable CSS grid using jQuery UI. $( function() { $( "#sortable" ).sortable({ handle: "handle" }); }); gridHolder{ display:grid; background:tan; grid-template-columns: ...

The `getScript` function in jQuery is not working as expected, even though the path is accurate and the script was successfully downloaded

Recently, I created a website using Mustache.js and incorporated templates loaded via AJAX with jQuery. During the testing phase on my local environment, everything worked perfectly. However, upon uploading the site to my school server, an issue arose whe ...

The JSON response may be missing, but the URL is still echoing the correct information

On a webpage with multiple forms, I'm utilizing jQuery to AJAX a form. Here's the function I'm using, which is basically a wrapper for the $.ajax function: function do_json_get(uri){ var ret = ''; var url = AJAX_URL + uri; ...

Select2 eliminates every tag except the first one

I am currently utilizing Select2 in Django for handling many-to-many relationships. The best approach I have found to handle all validation constraints is to create related objects through an AJAX request as soon as they are entered into the Select2 tag fi ...

What is the best way to arrange map items using justify-content-around for optimal display?

I'm currently iterating through products using the map function, but I'm having trouble getting the justify-content-around property to apply. I am working on a React project with Bootstrap 4 integrated. Below is the code snippet: {props.product ...

What is the best way to layer a background image with a CSS gradient?

I've spent over an hour searching for the right answer with no luck, so I'm turning to you for help. On my main page, there's a div with a large image that looks like this: <div id="hero"></div> It's working perfectly at t ...

Tips for integrating CSS keyframes in MUI v5 (using emotion)

Hey there! I'm currently working on adding some spinning text, similar to a carousel, using keyframes in my React app. The setup involves MUI v5 with @emotion. Basically, I want a "box" element to show different words every few seconds with a rotating ...

Troubleshooting Issue: Google Map not resizing correctly when window size changes

Utilizing media queries, I have implemented an adaptive layout on my website. However, when I adjust the size of the browser window beyond a specific media query, the google map begins to malfunction (refer to the image) My Solution: CSS: #googleMap2{d ...

Flexbox parent div experiencing overflow due to horizontal margins protruding beyond the boundaries

Experiencing unexpected margin behavior with flex and seeking help for clarification. Here is a simple HTML structure I am using: <div className="dashboard"> <div className="dashboard__inner-container">Inner Container</div> </di ...

Add a close event to a list item in JQuery before

When I click on "+ List Item" or press enter in the input box, I want to add a new list item with a checkbox, input box, and a close button. The functionality should be similar to Google Keep's checked item event working in notes. Currently, the code ...

MaterialUI AppBar is missing a crucial element

I recently dived into the world of Material UI for React and it's been a good experience so far. However, I've noticed a white gap in my header that I can't seem to get rid of. https://i.sstatic.net/zAZJu.png Here is the snippet from my in ...

Looking to change the input field names by increasing or decreasing when clicking on a div using jQuery?

As a beginner in the world of jQuery, I am working on mastering some basic concepts. Currently, my goal is to create auto incrementing/decrementing input field names within a 'div' when clicking on an add/remove button. Below is the HTML code I a ...

Is there a more streamlined approach to coding in PHP and jQuery?

My PHP script: <?php $data = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml'); $xml = simplexml_load_string($data); $data1 = file_get_contents('http://www.skysports.com/rss/0,20514,11661,00.xml ...

"Troubleshooting an issue with mod_rewrite affecting the functionality of an ajax

The issue arises when the ajax request is not triggered after the URL has been rewritten using mod_rewrite. However, by simply removing the trailing slash from the link, the functionality works as expected. For example: It works with these URLs http:// ...

Switch out the ajax data in the input field

Is there a way to update the value in a text box using Ajax? Below is my code snippet: <input type="text" id="category_name" name="category_name" value="<?php if(isset($compName)) { echo ucfirst($compName); ...

Click on a Marker to automatically zoom to its location using Leaflet mapping technology

I have successfully implemented a feature to display markers on the map from a geojson file. Currently, when I hover over a marker, I can see its properties in a popup. However, I now want to enhance this functionality so that when a user clicks on a mar ...

Disabling comments is creating problems with the appearance of Wordpress pages

Visit handheldtesting.com Whenever the option "disable comments" is selected in the backend, or if 'display:none:' is added <div id="respond" class="comment-respond" style="display:none;"> to the code, half of the content on the page dis ...