按钮在我调整页面大小时会执行滑入和滑出动画(我不希望它这样做)。

huangapple go评论48阅读模式
英文:

Button does slide-in and -out animations when I resize page (I don't want it to do that)

问题

我正在尝试为我的网站制作一个导航按钮,仅在网站达到特定尺寸时才会出现。当需要时它会消失和重新出现,但会执行一种滑入和滑出的动画,这绝对不是我编写的,并且在消失之前还会停留大约 0.5 秒。导航按钮还连接了一个简单的 JavaScript,用于弹出覆盖菜单。我正在努力摆脱尺寸调整动画和 0.5 秒的停留时间。

关于按钮的 HTML 代码:

<button id="navbutton" onclick="openNav()">导航</button>

与导航按钮相关的 CSS 样式(注意:我学习网站编码的所有知识都是在过去的 24 小时内学到的 - 如果代码看起来混乱,那是因为我不知道我在做什么):

#navbutton {
    width: 70px;
    visibility: hidden;
    background-color: #FFF6EA;
    color: #545454;
    border: 2px solid #545454;
    padding: 13px;
    text-align: center;
    text-decoration: none;
    float: right;
    font: bold 16px/10px "IBM Plex Serif", serif;
    margin: 4px 2px;
    margin-left: 10px;
    cursor: pointer;
    border-radius: 30px;
}

#navbutton:hover {
    background-color: #545454;
    transition-duration: 0.4s;
    color: #FFF6EA;
}

@media screen and (max-width: 850px) {
    #navbutton {
        visibility: visible;
        margin-top: 20px;
    }
}

连接到覆盖菜单的 JavaScript 代码("navigation" div 和 "closeNav" 按钮没有在此处链接;我不认为 .js 与不必要的导航按钮动画有关,但无论如何,我还是添加了它以防万一):

function openNav() {
    document.getElementById("navigation").style.display = "100%";
}

function closeNav(){
    document.getElementById("navigation").style.display = "0%";
}

此外,当我点击导航按钮时,它目前不执行任何操作,尽管几个小时前还有效。我可能不小心删除了一个关键的代码片段,但这不是这个问题的重点 - 我会自己解决 / 在另一个帖子中问。但如果有任何异常情况,欢迎指出。

我尝试在 navbutton 可见后再添加额外的 float: right;,尽管从逻辑上讲这不会产生任何效果,因为它已经在类中(但人有时会不顾一切)。我还尝试了 position: absolute;(和 relative),因为我以为“也许这样会让它保持在原地不动”,但动画仍然存在。我还尝试删除 transition-duration,但没有奏效。

对于能够提供见解的人:我将欠你我的第一个孩子。谢谢。

英文:

I'm trying to make a navigation button for my website that only appears when the site's at a certain size. It disappears and reappears when needed, but does a slide-in and -out animation that I definitely didn't code, and also lingers for ~0.5 seconds before disappearing. The nav button is also connected to simple Javascript that's supposed to bring up a pop-up overlay menu. I'm trying to get rid of both the resizing animation and the lingering 0.5 seconds.

The HTML for the button:

&lt;button id=&quot;navbutton&quot; onclick=&quot;openNav()&quot;&gt;nav&lt;/button&gt;

The CSS relating to the navbutton (note: everything I know about coding a website, I learned within the past 24 hours - if the code looks messy, that's because I don't know what I'm doing):

#navbutton {
    width: 70px;
    visibility: hidden;
    background-color: #FFF6EA;
    color: #545454;
    border: 2px solid #545454;
    padding: 13px;
    text-align: center;
    text-decoration: none;
    float: right;
    font: bold 16px/10px &quot;IBM Plex Serif&quot;, serif;
    margin: 4px 2px;
    margin-left: 10px;
    cursor: pointer;
    border-radius: 30px;
}

#navbutton:hover {
    background-color: #545454;
    transition-duration: 0.4s;
    color: #FFF6EA;
}

@media screen and (max-width: 850px) {
    #navbutton {
        visibility: visible;
        margin-top: 20px;
    }
  }

The Javascript that connects to the overlay menu (the "navigation" div and "closeNav" button aren't linked here; I don't think the .js has anything to do with the unwarranted navbutton animations, but I'm adding it anyway in case it does):

function openNav() {
    document.getElementById(&quot;navigation&quot;).style.display = &quot;100%&quot;;
}

function closeNav(){
    document.getElementById(&quot;navigation&quot;).style.display = &quot;0%&quot;;
}

Also, the nav button currently doesn't do anything when I click it, despite it working a couple hours ago. I probably accidentally removed a detrimental piece of code, but that's not the point of this question - I'll figure it out/ask in another post. But if anything seems out of the ordinary, feel free to point it out.

I tried putting an extra float: right; after the navbutton turns visible, even though I know logically it doesn't do anything since it's already in the class (but a guy's desperate). I've also tried position: absolute; (and relative), because I thought "maybe this will make it stay in place and not move," but the animation is still there. I also tried deleting the transition-duration, but it didn't work.

To whoever can offer insight: I will owe you my firstborn. Thanks.

答案1

得分: 1

如果您没有指定transition-property的值,那么默认值是all,这意味着在过渡之前和之后具有不同值的所有属性都将被动画化。但是,如果您明确设置了transition-property的值,那么只有指定的属性将被动画化。所以我认为这可能是您的问题。尝试将过渡属性仅设置为background-colorcolor

#navbutton {
  width: 70px;
  visibility: hidden;
  background-color: #FFF6EA;
  color: #545454;
  border: 2px solid #545454;
  padding: 13px;
  text-align: center;
  text-decoration: none;
  float: right;
  font: bold 16px/10px "IBM Plex Serif", serif;
  margin: 4px 2px;
  margin-left: 10px;
  cursor: pointer;
  border-radius: 30px;
  transition-property: background-color, color;
  transition-duration: 0.4s;
}

#navbutton:hover {
  background-color: #545454;
  color: #FFF6EA;
}

希望这可以帮助您解决问题。

英文:

If you don't specify a transition-property value, the default value is all, which means that all properties that have a different value before and after the transition will be animated. However, if you explicitly set a transition-property value, only the specified properties will be animated. So I think this is your problem.Try setting the transition properties to only background-color and color.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function openNav() {
    document.getElementById(&quot;navigation&quot;).style.display = &quot;100%&quot;;
}

function closeNav(){
    document.getElementById(&quot;navigation&quot;).style.display = &quot;0%&quot;;
}

<!-- language: lang-css -->

#navbutton {
  width: 70px;
  visibility: hidden;
  background-color: #FFF6EA;
  color: #545454;
  border: 2px solid #545454;
  padding: 13px;
  text-align: center;
  text-decoration: none;
  float: right;
  font: bold 16px/10px &quot;IBM Plex Serif&quot;, serif;
  margin: 4px 2px;
  margin-left: 10px;
  cursor: pointer;
  border-radius: 30px;
  transition-property: background-color, color;
  transition-duration: 0.4s;
}

#navbutton:hover {
  background-color: #545454;
  color: #FFF6EA;
}

@media screen and (max-width: 850px) {
  #navbutton {
      visibility: visible;
      margin-top: 20px;
  }
}

<!-- language: lang-html -->

&lt;button id=&quot;navbutton&quot; onclick=&quot;openNav()&quot;&gt;nav&lt;/button&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 04:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75465235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定