在点击 JavaScript 函数之前,是否有一种自动设置 .style 的方法?

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

Is there a way to set a .style automatically before onClick js function?

问题

我正在为我正在构建的网站制作响应式导航。我正在使用transform: translateX CSS属性来对齐侧边栏,其中我使用两个JavaScript函数(showMenu / hideMenu)来更改CSS属性中的变量。我遇到的问题是,当在实时服务器中重新加载移动维度时,侧边栏要么自动可见,要么显示几秒钟,我认为这是由于我在CSS中设置的过渡时间导致的。

我认为一个可能有效的解决方案是在加载到页面时在初始showMenu按钮单击之前设置一个值,但是由于我仍处于学习JavaScript的初级阶段,因此无法正确思考要执行此操作的语法。我查看了多个网站的源代码以及YouTube上的响应式导航栏教程,但尚未找到任何有用的信息。在CSS中设置初始transform: translateX值不起作用。以下是代码:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var navLinks = document.getElementById("navLinks");

function showMenu() {
  navLinks.style.transform = "translateX(0%)";
}

function hideMenu() {
  navLinks.style.transform = "translateX(100%)";
}
<!-- language: lang-css -->
.navlinks {
  display: flex;
  position: absolute;
  inset: 0 0 0 50%;
  background: hsl(0, 0%, 80%);
  z-index: 2;
  text-align: left;
  transform: translateX(100%);
  transition: 0.5s;
}
<!-- language: lang-html -->
<nav class="navbar">
  <div class="brand-title">
    <h4>Bottoms Up Tiki Tours</h4>
  </div>

  <div id="navLinks" class="navlinks">
    <div class="x" onClick="hideMenu()">
      <i class="bi bi-x"></i>
    </div>

    <ul class="navlist">
      <li class="active"><a href="#home">Home</a></li>
      <li><a href="#prices">Prices</a></li>
      <li><a href="#about">About</a></li>
      <li><a href="#booking">Booking</a></li>
      <li><a href="https://www.facebook.com/bottomsuptikitours"><svg 
                 xmlns="http://www.w3.org/2000/svg" width="13" height="13" 
                 fill="currentColor" class="bi bi-facebook" viewBox="0 0 16 
                 16"><path d="M16 8.049c0-4.446-3.582-8.05-8-8.05C3.58 0-.002 
                 3.603-.002 8.05c0 4.017 2.926 7.347 6.75 7.951v-5.625h- 
                 2.03V8.05H6.75V6.275c0-2.017 1.195-3.131 3.022-3.131.876 0 
                 1.791.157 1.791.157v1.98h-1.009c-.993 0-1.303.621-1.303 
                 1.258v1.51h2.218l-.354 2.326H9.25V16c3.824-.604 6.75-3.934 
                 6.75-7.951z"></path></svg></a></li>
    </ul>
  </div>

  <div class="burger" onClick="showMenu()">
    <i class="bi bi-list"></i>
  </div>

</nav>
<!-- end snippet -->
英文:

I am working on a responsive nav for a website I am building. I am using a transform:translateX css property to align the sidebar, where I use two js functions (show/hideMenu) to change the variable in the css property. The issue I am finding is that when reloading the mobile dimensions in the live server, the sidebar is either visible automatically or shows for a few seconds, which I assume is due to the transition time I have set in css.

A solution I feel may work would be have a value set before the initial showMenu button click upon loading into the page, however I am still in the beginning stages of learning js so I cant think of the syntax to do this correctly. Ive looked at multiple source codes for websites, as well as responsive navbar tutorials on youtube and have yet to find anything that helps. Setting the initial transform:translateX value in css doesnt work. Here is the code:

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

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

var navLinks = document.getElementById(&quot;navLinks&quot;);
function showMenu() {
navLinks.style.transform = &quot;translateX(0%)&quot;;
}
function hideMenu() {
navLinks.style.transform = &quot;translateX(100%)&quot;;
}

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

.navlinks {
display: flex;
position: absolute;
inset: 0 0 0 50%;
background: hsl(0, 0%, 80%);
z-index: 2;
text-align: left;
transform: translateX(100%);
transition: 0.5s;
}

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

&lt;nav class=&quot;navbar&quot;&gt;
&lt;div class=&quot;brand-title&quot;&gt;
&lt;h4&gt;Bottoms Up Tiki Tours&lt;/h4&gt;
&lt;/div&gt;
&lt;div id=navLinks class=&quot;navlinks&quot;&gt;
&lt;div class=&quot;x&quot; onClick=&quot;hideMenu()&quot;&gt;
&lt;i class=&quot;bi bi-x&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;ul class=&quot;navlist&quot;&gt;
&lt;li class=&quot;active&quot;&gt;&lt;a href=&quot;#home&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li class=&gt;&lt;a href=&quot;#prices&quot;&gt;Prices&lt;/a&gt;&lt;/li&gt;
&lt;li class=&gt;&lt;a href=&quot;#about&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
&lt;li class=&gt;&lt;a href=&quot;#booking&quot;&gt;Booking&lt;/a&gt;&lt;/li&gt;
&lt;li class=&gt;&lt;a href=&quot;https://www.facebook.com/bottomsuptikitours&quot;&gt;&lt;svg 
xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;13&quot; height=&quot;13&quot; 
fill=&quot;currentColor&quot; class=&quot;bi bi-facebook&quot; viewBox=&quot;0 0 16 
16&quot;&gt;&lt;path d=&quot;M16 8.049c0-4.446-3.582-8.05-8-8.05C3.58 0-.002 
3.603-.002 8.05c0 4.017 2.926 7.347 6.75 7.951v-5.625h- 
2.03V8.05H6.75V6.275c0-2.017 1.195-3.131 3.022-3.131.876 0 
1.791.157 1.791.157v1.98h-1.009c-.993 0-1.303.621-1.303 
1.258v1.51h2.218l-.354 2.326H9.25V16c3.824-.604 6.75-3.934 
6.75-7.951z&quot;/&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div class=&quot;burger&quot; onClick=&quot;showMenu()&quot;&gt;
&lt;i class=&quot;bi bi-list&quot;&gt;&lt;/i&gt;
&lt;/div&gt;
&lt;/nav&gt;

<!-- end snippet -->

答案1

得分: 1

尝试使用 CSS,例如创建一个按钮,当鼠标悬停在上面时,通过过渡从某个属性更改 CSS 属性,从某个值到另一个值。

英文:

Instead try using css like create button that when hovered on change the css property using transition from, to

答案2

得分: -1

我最终超出了我的能力范围,并用JS使它复杂化了,但这有助于我理解JS。我使用了aria-expanded状态来在单击时设置属性。这解决了我的问题。如果有人对如何做到这一点有任何问题,请随时提问。

英文:

I ended up going way over my head and complicating it with JS but it helped with my understanding of JS. I used the aria-expanded states to set attributes upon click. This solved my issue. If anyone has any questions on how to do this feel free to ask.

huangapple
  • 本文由 发表于 2023年3月12日 12:22:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711028.html
匿名

发表评论

匿名网友

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

确定