javascript - 打开按钮时停止图像移动

标签 javascript html css

我希望图像在我打开按钮时停止向下移动。我尝试将图像的位置更改为固定,但随后图像从我的页面上消失了,我不确定它去了哪里。非常感谢任何有关如何解决此问题的建议。

.image3 {
  position: fixed;
}

.collapsible {
  background-color: #C5E3ED;
  color: black;
  padding: 15px;
  width: 400px;
  border: none;
  text-align: left;
  outline: none;
  font-size: 20px;
  font-family:apple chancery;
  border:1px solid rgba(0,0,0,0.1);
  border-radius:5px;
}

.active, .collapsible:hover {
  background-color: #8bc7db;
}

.content {
  padding: 0 18px;
  max-height: 0;
  height: 300px;
  overflow: scroll;
  transition: max-height 0.2s ease-out;
  background-color: #C5E3ED;
  width: 365px;
}

<script>
var coll = document.getElementsByClassName("collapsible");
var i;

for (i = 0; i < coll.length; i++) {
  coll[i].addEventListener("click", function() {
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.maxHeight){
      content.style.maxHeight = null;
    } else {
      content.style.maxHeight = content.scrollHeight + "px";
    } 
  });
}
</script>
  <button class="collapsible">title</button>
  <div class="content">
    <p>text</p>
  </div>
<img src="https://www.w3schools.com/Bootstrap/paris.jpg" alt="wolf" class="image3">

最佳答案

尝试使用 position:fixed 而不是 display:fixeddisplay:fixed 不存在。 display 有 block、inline、none、inline-block 等选项。

另外,如果您使用这样的边距,您可能没有以正确的方式编码。如果您希望它成为内联元素,我会研究使用自动边距和 float 。

编辑:看到你的评论,因为你想要一个临时解决方案,你可以在点击按钮时向上移动它。

button:hover:active { margin-top:-5px; }

关于javascript - 打开按钮时停止图像移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52600989/

相关文章:

javascript - 如何安排我的 div 标签?

php - Wordpress 子主题不会覆盖父主题 css

javascript - 如何在不使用浏览器内置PDF查看器的情况下在网络上查看PDF?

javascript - HTML anchor 发送请求但不点击链接

javascript - 带有多个按钮的 document.getElementById

php - MPDF 复选框未在 PDF 中仅显示点

javascript - ActiveXObject运行不返回(shell)

javascript - 如何在不禁用所有文本选择的情况下使用 shift 禁用文本选择?

html - 如何使 Font Awesome 图标以特定颜色发光

jquery - 如何获取隐藏的绝对div的高度