【问题标题】:Text appear/disappear on top of image with button toggle文本出现/消失在带有按钮切换的图像顶部
【发布时间】:2022-01-11 23:05:28
【问题描述】:

在移动设备中,我正在尝试创建一个显示在图像顶部的切换开关,当点击它时,文本也会显示在图像顶部。

我基本上想重新创建The Guardian newspaper 如何处理移动设备右下角的小 (i) 图标。

在桌面上,文本默认在图像下方,并且 (i) 图标消失了。

到目前为止,我已经设法在网上其他地方找到了类似的解决方案,但它并没有像我需要的那样正常工作。

function toggleText() {
   var text = document.getElementById("demo");
   if (text.style.display === "none") {
      text.style.display = "block";
   } else {
      text.style.display = "none";
   }
}
#blog {
    width: 100%;
}

#blog figure {
    position: relative;
}

#blog figure figcaption {
    display: none;
    position: absolute;
    bottom: 0;
    left: 0;
    box-sizing: border-box;
    width: 100%;
    color: black;
    text-align: left;
    background: rgba(0, 0, 0, 0.5);
}

#blog figure button {
    position: absolute;
    bottom: 0;
    right: 0;
    color: black;
    border: 5px solid black;
}
<div id="blog">
  <figure> 
    <img src="https://cdn2.hubspot.net/hubfs/4635813/marble-around-the-world.jpg" alt="A photo of a slab of marble for example">
    <figcaption id="demo" style='display: none'>A photo of a slab of marble for example</figcaption>
    <button type='button' onclick="toggleText()">(i)</button>
  </figure>
</div>

【问题讨论】:

  • 停止使用内联 JS on* 属性处理程序,就像您希望不要使用内联 style 属性一样(编辑:哎呀!)。 JS 和 CSS 应该只在一个地方,它们是各自的文件或标签。请改用.addEventListener()#demo { display: none; }
  • 我知道,我也不想这样做,但如果没有那种内联显示样式:无,我必须单击/点击按钮两次才能显示标题,不理想.. .
  • 你所说的不是真的。内联 display: none; 等于来自 CSS 文件样式表的 display: none;。想想吧。
  • 如果你继续这个Fiddle 你会明白我的意思。实际上,with 内联样式属性 display:none 点击按钮会显示 figcaption(应该如此)——display:none 也在 CSS 中。如果我删除 display:none 的内联样式属性并再次运行小提琴,当我第一次单击按钮时,figcaption 不会出现,但在第二次单击时会出现。我已经重新运行小提琴几次,这就是发生的事情。我不是知识最渊博的开发人员,但据我所知,必须先初始化一些东西......
  • 在这种情况下,不要检查el.style.display === "something" ,而是使用developer.mozilla.org/en-US/docs/Web/API/Window/…,或者直接使用el.classList.toggle("someClassName", boolean)

标签: javascript html css


【解决方案1】:
  • 不要使用 ID。您的代码应该是可重用的!
  • 不要使用内联 JS on* 处理程序,而是使用 Element.addEventListener()
  • 不要使用内联 style 属性。
  • 不要使用el.style.display === "something" 来检查display 样式。请改用Element.classList.toggle()

这个简单的示例使用 JavaScript 简单地在按钮的父级 figure 元素上切换 className "is-active"
其他一切(图标符号更改、字幕动画等)完全由 CSS 处理:

document.querySelectorAll("figure button").forEach(EL_btn => {  
  EL_btn.addEventListener("click", () => {
    EL_btn.closest("figure").classList.toggle("is-active");
  });
});
/* QuickReset */ * {margin: 0; box-sizing: border-box;}

img {
  max-width: 100%; /* Never extend images more than available */
}

figure {
  position: relative;
  overflow: hidden; /* overflow hidden to allow figcaption hide bottom */
}

figure img {
  display: block; /* prevent "bottom space" caused by inline elements */
}

figure figcaption {
  position: absolute;
  width: 100%; 
  bottom: 0;
  padding: 1rem;
  padding-right: 4rem; /* Prevent text going under the button icon */ 
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
  transform: translateY(100%); /* Move down, out of view */
  transition: transform 0.3s; /* Add some transition animation */
}

figure.is-active figcaption {
  transform: translateY(0%); /* Move into view */
}

figure button {
  position: absolute;
  width: 2rem;
  height: 2rem;
  bottom: 0.5rem;
  right: 0.5rem;
  border-radius: 50%;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
  border: 0;
  cursor: pointer;
}
figure button::before {
  content: "\2139"; /* i icon */
}

figure.is-active button::before {
  content: "\2A09"; /* x icon */
}
<figure>
  <img src="https://cdn2.hubspot.net/hubfs/4635813/marble-around-the-world.jpg" alt="A photo of a slab of marble for example">
  <figcaption>A photo of a slab of marble for example</figcaption>
  <button type="button"></button>
</figure>

以上内容适用于您网站上任意数量的此类元素,无需添加更多 CSS 或 JS。

【讨论】:

  • 非常感谢你的朋友!!!我可以看到这是更好的方法!我可以最后一次请求您的支持吗(我希望!)?我已将您的示例应用到 test post on my site,但我仍然遇到了 figcaption 关闭的问题,从外观上看,它并不完全位于 bottom: 0。我有样式重置(我认为这是罪魁祸首),但我已经摸不着头脑了几个小时,我无法修复那个 figcaption 关闭...
  • 其实我可以看到代码sn-p发生了同样的事情,就像figcaption在图片下方弹出-5px,而不是bottom: 0
  • @liamjmoore 这导致图像默认为inline。请改用display: block;。也会修复我的演示。
  • 如此简单——天啊!!!再次感谢 Roko,非常感谢!
  • @liamjmoore 不客气!快乐编码☕
【解决方案2】:

我看到有几件事可能会搞砸这件事,一个是没有任何东西可以让您的图像适应您的移动屏幕,此外还有默认情况下存在的边距,所以我建议进行这些更改到 CSS:

首先我将 box-sizing 设置为 border-box 并将边距设置为 0,顺便说一下,这应该是一种常规做法。

*{

  box-sizing: border-box;
  margin: 0;

}

然后选择图像并使其适应您的页面


#blog figure img{
  height: auto;
   width:100%;
}

最后,对于一些样式,您可以在博客 div 中添加一些内边距,以使屏幕上的图像略小

#blog {
    width: 100%;
    padding: 35px;
}

这是Fiddle

【讨论】:

  • 我注意到标题和按钮比图像略低,可能想在标题和按钮中添加底部:0
  • @mok_ku 标题和按钮已经有那些 edit:这很可能是因为按钮边框离开边缘推动一些东西,做底部:4px;对于两者(这是边框的厚度)修复它恰到好处
  • 哇,谢谢卢锡安!我现在可以看到这几乎可以工作了!就mok_ku而言,我看到了底部:0并且我还删除了按钮周围的边框,但我仍然看到标题和按钮仍然在图像下方/之外几个像素......不要假设任何人也会知道如何解决它,以便文本更长(因为我一直在尝试寻找边缘情况)不会在按钮后面?
  • 如果有什么问题,我已经尝试将其正确应用到 my blog 不希望使用像素微调未对齐的标题(就像我现在所做的那样),我很“紧张” " 它可能在其他浏览器/设备中显示不正确...
猜你喜欢
  • 2019-02-03
  • 1970-01-01
  • 2020-09-12
  • 2021-12-23
  • 1970-01-01
  • 2012-06-09
  • 2015-02-07
  • 2023-04-07
  • 2013-09-07
相关资源
最近更新 更多