skip to Main Content

I am new to ajax I want to load images first and then footer but when I am trying to load the images using ajax the footer gets loaded first instead of the images which makes the image overlap the footer. As you can see in the below image.

<!doctype html>
<html lang="en">
  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
    <link rel="stylesheet" href="styles.css" />     
    <title>Image Gallery</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
        $.ajax({
            url: "js/data.json",
            dataType: "json",
            success: function(data)
            {
                $.each(data, function(i,pic){
                    $("ul.gallery").append("<li><img src='images/square/"+ pic.path + "' alt='" + pic.title + "'></li>");
                });
            },
            error: function() {
                console.log("Picture cannot be loaded");
            }
        });
    });
    </script>
    <style>
        .b-footer{
            background-color: #274472; 
            color:white;
        }
        .gallery {
            list-style-type: none;
            margin: 0px;
            padding: 0px;
        }
        .gallery {
            list-style-type: none;
        }

        .gallery li{
            float: left;
            padding: 30px;
            position: relative;
            overflow: hidden;
        }
    </style>
  </head>
  <body>
    <main class="container">     
        <ul class="gallery">
        </ul>
    </main>
    <footer class="text-center p-4 b-footer">
        This is a footer
        <div>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb justify-content-center">
                    <li class="breadcrumb-item"><a href="#">Home</a></li>
                    <li class="breadcrumb-item"><a href="#">About</a></li>
                    <li class="breadcrumb-item"><a href="#">Contact</a></li>
                    <li class="breadcrumb-item"><a href="#">Browse</a></li>
                </ol>
            </nav>
        </div>
    </footer> 
  </body>
</html>

enter image description here

Can anyone help me out with how to resolve this issue like how to make the footer to be loaded later after the images are loaded? A correct code can help me.

4

Answers


  1. Try this in your website

    <!doctype html>
    <html lang="en">
      <head>
    
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
        <link rel="stylesheet" href="styles.css" />     
        <title>Image Gallery</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
            $.ajax({
                url: "js/data.json",
                dataType: "json",
                success: function(data)
                {
                    $.each(data, function(i,pic){
                        $("ul.gallery").append("<li><img src='images/square/"+ pic.path + "' alt='" + pic.title + "'></li>");
                    });
                },
                done: function() {
                    $("#footer").show();
                },
                error: function() {
                    console.log("Picture cannot be loaded");
                }
            });
        });
        </script>
        <style>
            .b-footer{
                background-color: #274472; 
                color:white;
            }
            .gallery {
                list-style-type: none;
                margin: 0px;
                padding: 0px;
            }
            .gallery {
                list-style-type: none;
            }
    
            .gallery li{
                float: left;
                padding: 30px;
                position: relative;
                overflow: hidden;
            }
        </style>
      </head>
      <body>
        <main class="container">     
            <ul class="gallery">
            </ul>
        </main>
        <footer class="text-center p-4 b-footer" id="footer" style="display:none;">
            This is a footer
            <div>
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb justify-content-center">
                        <li class="breadcrumb-item"><a href="#">Home</a></li>
                        <li class="breadcrumb-item"><a href="#">About</a></li>
                        <li class="breadcrumb-item"><a href="#">Contact</a></li>
                        <li class="breadcrumb-item"><a href="#">Browse</a></li>
                    </ol>
                </nav>
            </div>
        </footer> 
      </body>
    </html>
    Login or Signup to reply.
  2. The simple answer is to remove existing styles for .gallery li and add the below

    .gallery li {
        display: inline-block;
        padding: 30px;
        position: relative;
        overflow: hidden; }
    
    Login or Signup to reply.
  3. I think your footer would be fine if you added a clearfix class to your main container. The reason is that all your li’s are float left so your footer will overlap the main container.

    .clearfix::after { 
        content: ""; 
        clear: both; 
        display: block; 
    }
    

    SUMMARY

    The clearfix, for those unaware, is a CSS hack that solves a
    persistent bug that occurs when two floated elements are stacked next
    to each other. When elements are aligned this way, the parent
    container ends up with a height of 0, and it can easily wreak havoc on
    a layout.

    https://css-tricks.com/clearfix-a-lesson-in-web-development-evolution/

    Login or Signup to reply.
  4. It’s not an ajax issue you just need to add proper footer CSS here is working solution of your problem just replace footer CSS with this

    .b-footer{
                    background-color: #274472; 
                    color:white;
                    position: absolute;
                     bottom: 0;
                    width: 100%;
                } 
    
    <!doctype html>
    <html lang="en">
      <head>
    
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    
        <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-F3w7mX95PdgyTmZZMECAngseQB83DfGTowi0iMjiWaeVhAn4FJkqJByhZMI3AhiU" crossorigin="anonymous">
        <link rel="stylesheet" href="styles.css" />     
        <title>Image Gallery</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kQtW33rZJAHjgefvhyyzcGF3C5TFyBQBA13V1RKPf4uH+bwyzQxZ6CmMZHmNBEfJ" crossorigin="anonymous"></script>
        <script>
            $(document).ready(function(){
             $("ul.gallery").append("<li><img src='https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQtfeR2gr_Z2U5rNiTKieMXMM9ZY96GbKUQQg&usqp=CAU'></li>");
            $.ajax({
                url: "js/data.json",
                dataType: "json",
                success: function(data)
                {
                  
                       
                 
                },
                error: function() {
                     
                }
            });
        });
        </script>
        <style>
            .b-footer{
                background-color: #274472; 
                color:white;
                position: absolute;
                    bottom: 0;
                       width: 100%;
            }
            .gallery {
                list-style-type: none;
                margin: 0px;
                padding: 0px;
            }
            .gallery {
                list-style-type: none;
            }
    
            .gallery li{
                float: left;
                padding: 30px;
                position: relative;
                overflow: hidden;
            }
            .clearfix::after { 
        content: ""; 
        clear: both; 
        display: block; 
    }
        </style>
      </head>
      <body>
        <main class="container">     
            <ul class="gallery">
            </ul>
        </main>
        <footer class="text-center p-4 b-footer">
            This is a footer
            <div id="footer">
                <nav aria-label="breadcrumb">
                    <ol class="breadcrumb justify-content-center">
                        <li class="breadcrumb-item"><a href="#">Home</a></li>
                        <li class="breadcrumb-item"><a href="#">About</a></li>
                        <li class="breadcrumb-item"><a href="#">Contact</a></li>
                        <li class="breadcrumb-item"><a href="#">Browse</a></li>
                    </ol>
                </nav>
            </div>
        </footer> 
      </body>
    </html>
      </body>
    </html>

    Note:- for proper results see in full page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search