javascript - 为什么我的轮播图像旁边有额外的空间?

标签 javascript html css spacing

我不是一名编码员,但正在为我们的网站编写一个图像轮播。除了我在间距方面遇到的最后一个奇怪的问题之外,我已经完成了所有工作。在所附的图像中,您会看到屏幕截图与其右侧的下一个图像按钮之间的间距太大。

Here is a screenshot of the spacing problem

这是代码(提前道歉,这真的很糟糕):

// Declaring variable array of images. You can put as many as you want.
const myimages = ["https://www.agathos.io/hs-fs/hubfs/text%20v2%20gimp.png?width=534&height=1136&name=text%20v2%20gimp.png", "https://i.imgur.com/uoAHQ17.jpeg", "https://i.imgur.com/rk3KK2p.jpeg", "https://i.imgur.com/rk3KK2p.jpeg", "https://i.imgur.com/rk3KK2p.jpeg"];
const mycaptions = ["Once a week, physicians receive a text letting them know specific information about how they are handling a particular practice area. Curious to learn more, they follow the link for a deeper look. What is nice about this approach is that it is easy and can be accessed at a time convenient to physician.", "Test caption 2", "Test caption 3", "Test caption 4", "Test caption 5"]; 

const prevBtn = document.getElementById("p-10-s-i-s-prev-btn");  // assigning variable for previous button
const nextBtn = document.getElementById("p-10-s-i-s-next-btn");  // assigning variable for next button
const imageContainer = document.getElementById("p-10-s-i-s-image-container"); // assigning variable for image container div
const captionContainer = document.getElementById("p-11-s-i-s-caption-place-holder");
var myimage = myimages[0]; // Assigning initial value for the varibale to show on page loading and showing first image.
var mycaption = mycaptions[0]; // Assigning and showing the first caption of the first image.

imageContainer.innerHTML = '<img src="'+myimage+'" />';
captionContainer.innerHTML = mycaption;


var counter = 0;
prevBtn.addEventListener("click", function(){
    if (counter > 0 && counter < myimages.length){
            counter--;
            myimage = myimages[counter];
            mycaption = mycaptions[counter];
            imageContainer.innerHTML = '<img src="'+myimage+'" />';
            captionContainer.innerHTML = mycaption;
        }
    });

nextBtn.addEventListener("click", function(){
    if (counter < myimages.length-1){
        counter++;
        myimage = myimages[counter];
        mycaption = mycaptions[counter];
        imageContainer.innerHTML = '<img src="'+myimage+'" />';
        captionContainer.innerHTML = mycaption;
    }
});
.p-10-s-i-s-page-background{
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.p-10-simple-image-slider-wrapper{
    max-width: 45%;
    height: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 20px;
}

#p-10-s-i-s-image-container{
    max-width: 70%;
    height: auto;
    display: flex;
    justify-content: space-between;
}

#p-10-s-i-s-image-container img{
    max-width: 70%;
    height: auto;
    display: flex;
    align-items: center;
    animation: p-10-image-animation 1s;
    
}

#p-10-s-i-s-prev-btn, #p-10-s-i-s-next-btn{
    width: 50px;
    font-family: 'Open Sans', sans-serif;
    font-size: 50px;
    display: flex;
    flex: 1;
    align-items: center;
    cursor: pointer;
    color: orange;
}

#p-10-s-i-s-prev-btn:hover, #p-10-s-i-s-next-btn:hover{
    
    transition: all 1s;
    
}

.p-10-s-i-s-page-background h1{
    color: rgb(243, 236, 176);
}

@keyframes p-10-image-animation{
    0%{
        opacity: 0.5;
    }
    100%{
        opacity: 1;
    }
}

#p-11-s-i-s-caption-place-holder{
    padding: 5px 150px 5px 150px;
    font-family: 'Open Sans', sans-serif;
    color: #565555;
    text-align: left;
    font-size: 18px;
    line-height: 150%;
    margin-bottom: 20px;
}

.yvanaplaceholder{
  display: flex;
  text-align: center;
  justify-content: center;
}

.yvana{
  color: darkgreen;
  text-decoration: none;
 }

.name{
  color: crimson;
}
<div class="p-10-s-i-s-page-background">
    <div id="p-11-s-i-s-caption-place-holder">
    </div>
  
  <div class="p-10-simple-image-slider-wrapper">
        <div id="p-10-s-i-s-prev-btn">&#60;</div>
        <div id="p-10-s-i-s-image-container" ></div>
        <div id="p-10-s-i-s-next-btn">&#62;</div>
    </div>
</div>

最佳答案

#p-10-s-i-s-image-container 中的 justify-content: space- Between 替换为 justify-content: center 将解决这个问题。

// Declaring variable array of images. You can put as many as you want.
const myimages = ["https://www.agathos.io/hs-fs/hubfs/text%20v2%20gimp.png?width=534&height=1136&name=text%20v2%20gimp.png", "https://i.imgur.com/uoAHQ17.jpeg", "https://i.imgur.com/rk3KK2p.jpeg", "https://i.imgur.com/rk3KK2p.jpeg", "https://i.imgur.com/rk3KK2p.jpeg"];
const mycaptions = ["Once a week, physicians receive a text letting them know specific information about how they are handling a particular practice area. Curious to learn more, they follow the link for a deeper look. What is nice about this approach is that it is easy and can be accessed at a time convenient to physician.", "Test caption 2", "Test caption 3", "Test caption 4", "Test caption 5"];

const prevBtn = document.getElementById("p-10-s-i-s-prev-btn"); // assigning variable for previous button
const nextBtn = document.getElementById("p-10-s-i-s-next-btn"); // assigning variable for next button
const imageContainer = document.getElementById("p-10-s-i-s-image-container"); // assigning variable for image container div
const captionContainer = document.getElementById("p-11-s-i-s-caption-place-holder");
var myimage = myimages[0]; // Assigning initial value for the varibale to show on page loading and showing first image.
var mycaption = mycaptions[0]; // Assigning and showing the first caption of the first image.

imageContainer.innerHTML = '<img src="' + myimage + '" />';
captionContainer.innerHTML = mycaption;


var counter = 0;
prevBtn.addEventListener("click", function() {
  if (counter > 0 && counter < myimages.length) {
    counter--;
    myimage = myimages[counter];
    mycaption = mycaptions[counter];
    imageContainer.innerHTML = '<img src="' + myimage + '" />';
    captionContainer.innerHTML = mycaption;
  }
});

nextBtn.addEventListener("click", function() {
  if (counter < myimages.length - 1) {
    counter++;
    myimage = myimages[counter];
    mycaption = mycaptions[counter];
    imageContainer.innerHTML = '<img src="' + myimage + '" />';
    captionContainer.innerHTML = mycaption;
  }
});
.p-10-s-i-s-page-background {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.p-10-simple-image-slider-wrapper {
  max-width: 45%;
  height: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

#p-10-s-i-s-image-container {
  max-width: 70%;
  height: auto;
  display: flex;
  justify-content: center;
}

#p-10-s-i-s-image-container img {
  max-width: 70%;
  height: auto;
  display: flex;
  align-items: center;
  /* changed */
  animation: p-10-image-animation 1s;
}

#p-10-s-i-s-prev-btn,
#p-10-s-i-s-next-btn {
  width: 50px;
  font-family: 'Open Sans', sans-serif;
  font-size: 50px;
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: orange;
}

#p-10-s-i-s-prev-btn:hover,
#p-10-s-i-s-next-btn:hover {
  transition: all 1s;
}

.p-10-s-i-s-page-background h1 {
  color: rgb(243, 236, 176);
}

@keyframes p-10-image-animation {
  0% {
    opacity: 0.5;
  }
  100% {
    opacity: 1;
  }
}

#p-11-s-i-s-caption-place-holder {
  padding: 5px 150px 5px 150px;
  font-family: 'Open Sans', sans-serif;
  color: #565555;
  text-align: left;
  font-size: 18px;
  line-height: 150%;
  margin-bottom: 20px;
}

.yvanaplaceholder {
  display: flex;
  text-align: center;
  justify-content: center;
}

.yvana {
  color: darkgreen;
  text-decoration: none;
}

.name {
  color: crimson;
}
<div class="p-10-s-i-s-page-background">
  <div id="p-11-s-i-s-caption-place-holder">
  </div>

  <div class="p-10-simple-image-slider-wrapper">
    <div id="p-10-s-i-s-prev-btn">&#60;</div>
    <div id="p-10-s-i-s-image-container"></div>
    <div id="p-10-s-i-s-next-btn">&#62;</div>
  </div>
</div>

关于javascript - 为什么我的轮播图像旁边有额外的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70946318/

相关文章:

javascript - 如何清除 firebase 上的 service-worker.js 缓存

javascript - 点击图像

javascript - 在带有 RequireJS 的 TypeScript 中使用 JQuery 等

javascript - Twitter Bootstrap 选项卡运行不正常

html - CSS 位置 Sticky 和 ​​Z-Index overlay/modal

javascript - 如何使用Electron菜单执行angular 7导航

html - 布局框在宽屏显示中插入,在窄屏(移动)显示中内嵌

html - <body> 拉伸(stretch)到 100% 的内容,而不是浏览器大小

jquery - 将 codepen 网站移植到 github 页面 - 折叠的导航按钮不起作用

javascript - 如何让我的子菜单在 jQuery 悬停菜单中保持打开状态 - 请检查示例