Home > front end >  How to fill the mobile window with image?
How to fill the mobile window with image?

Time:05-19

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Document</title>
    <link href="index.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="index.js"></script>
</head>
 
<body>
   
  <div >

    <div >
       <img src="bg.jpg"> 
    </div>
           
    <div >
        <img src="lemon.jpg"> 
    </div>
           
    <div >
        <img src="pear.webp"> 
    </div>

    <a  onclick="prevSlide()">&#10094;</a>
    <a  onclick="nextSlide()">&#10095;</a>
           
</div>
    
</body>
</html>
body{
    margin: 0;
}

/* Slideshow container */
.slideshow-container {
    /*max-width: 1440px;*/
    position: relative;
    margin: auto;
    margin-left: 0%;
    margin-top: 0%;
  }
  
  /* effect */
  .fade {
    -webkit-animation-name: fade;
    -webkit-animation-duration: 1.5s;
    animation-name: fade;
    animation-duration: 1.5s;
  }
  
  @-webkit-keyframes fade {
    from {opacity: .4} 
    to {opacity: 1}
  }
  
  @keyframes fade {
    from {opacity: .4} 
    to {opacity: 1}
  }
  
  /* Next & previous buttons */
  .prev, .next {
    cursor: pointer;
    position: absolute;
    top: 45%;   
    width: auto;
    padding: 16px;
    margin-top: -22px;
    color: red;
    font-weight: bold;
    font-size: 18px;
    transition: 0.6s ease;
    border-radius: 0 3px 3px 0;
  }
  
  /* Position the "next button" to the right */
  .next {
    right: 0%;
    border-radius: 3px 0 0 3px;
  }
  
  /* On hover, add a black background color with a little bit see-through */
  .prev:hover, .next:hover {
    background-color: rgba(0,0,0,0.8);
  }

img{
    width:100%;
    height: 30%important;
}
$(document).ready(function () {
    $(".mySlideDiv").not(".active").hide(); 
    
    setInterval(nextSlide, 4000); 
});

function prevSlide() {
    $(".mySlideDiv").hide(); 
    var allSlide = $(".mySlideDiv"); 
    var currentIndex = 0; 
    
    $(".mySlideDiv").each(function(index,item){ 
        if($(this).hasClass("active")) {
            currentIndex = index;
        }
        
    });
    
    var newIndex = 0;
    
    if(currentIndex <= 0) {
        newIndex = allSlide.length-1;
    } else {
        newIndex = currentIndex-1;
    }

    $(".mySlideDiv").removeClass("active");
    $(".mySlideDiv").eq(newIndex).addClass("active");
    $(".mySlideDiv").eq(newIndex).show();

}

function nextSlide() {
    $(".mySlideDiv").hide();
    var allSlide = $(".mySlideDiv");
    var currentIndex = 0;
    
    $(".mySlideDiv").each(function(index,item){
        if($(this).hasClass("active")) {
            currentIndex = index;
        }
        
    });
    
    var newIndex = 0;
    
    if(currentIndex >= allSlide.length-1) {
        newIndex = 0;
    } else {
        newIndex = currentIndex 1;
    }

    $(".mySlideDiv").removeClass("active");
    $(".mySlideDiv").eq(newIndex).addClass("active");
    $(".mySlideDiv").eq(newIndex).show();
    
}

Screenshot on mobile

I want to make a responsive web application, but mobile window isn't filled with the image, and I don't know how to edit the code to make it. I assume that I have to embed the code targeted with mobile web, but I don't know how to do. I attach the image file to explain my situation. Please help.

CodePudding user response:

You can try to put in the css of your container:

width:100%;
height: 100%;

And in the css of your pictures :

width:100%;
height: undefined;
// figure out your image aspect ratio
aspectRatio: 50 / 32;

CodePudding user response:

Add css with media query

@media(max-width:480px) {
img {
    width:100% !important;
    height: 100% !important;
    object-fit: cover; }
}
  • Related