JavaScript和媒体查询之间的”冲突”?

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

Javascript and media query "conflict"?

问题

以下是翻译后的内容:

<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-js -->

function showOrHideMenu(menuId) {
  // alert(menuId);
  var menu = document.getElementById(menuId);
  // alert(menu);
  var isVisible = menu.style.visibility;
  // alert(isVisible);

  if ((isVisible === 'hidden') || (isVisible === '')) {
    // alert(menu1);
    menu.style.visibility = 'visible';
    menu.style.width = '75vw';
    // alert("passe 1 bis");        
  } else {
    // alert("passe 2");
    menu.style.visibility = 'hidden';
    // alert("passe 2 bis");
  }
}

<!-- 语言: lang-css -->

@media (min-width: 800px) {
  .css_menu_1 {
    visibility: visible;
    font-size: 1.3rem;
    background-color: goldenrod;
  }
}

<!-- 语言: lang-html -->

<img src="../images/hamburger_menu_icon.png" alt="显示或隐藏菜单1" class="css_btn_menu_1" id="btn_menu_1" onclick="showOrHideMenu('menu_1');">

<!-- 结束代码片段 -->

当我在宽度小于800px的屏幕上时,我可以点击图像。
`showOrHideMenu()` 函数被执行,确实根据指定的方式更改了id为 `menu_1` 的HTML元素的可见性和宽度。
我再次点击图像,HTML元素如预期地隐藏。
现在,如果我增加视口的大小,HTML元素应该再次可见。
但实际上,我得到了一个空白空间。

执行Javascript函数是否与媒体查询的“执行”发生冲突?
我可以做些什么来修复这个问题?

-- 编辑 1 --

添加以下CSS:

```css
.css_menu_1.active 
{ 
    visibility : visible; 
    width: 75vw;
}

并用以下Javascript替换:

<script type="text/javascript">
    var btnMenu1 = document.getElementById('btn_menu_1');      
    var menu1 = document.getElementById('menu_1');       
    btnMenu1.addEventListener('click', () => 
    {
        menu1.classList.toggle('active');
    });
</script>

解决了我的问题。



<details>
<summary>英文:</summary>

Below is the code, the problem description and questions come right after.

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    function showOrHideMenu(menuId) {
      // alert(menuId);
      var menu = document.getElementById(menuId);
      // alert(menu);
      var isVisible = menu.style.visibility;
      // alert(isVisible);

      if ((isVisible === &#39;hidden&#39;) || (isVisible === &#39;&#39;)) {
        // alert(menu1);
        menu.style.visibility = &#39;visible&#39;;
        menu.style.width = &#39;75vw&#39;;
        // alert(&quot;passe 1 bis&quot;);        
      } else {
        // alert(&quot;passe 2&quot;);
        menu.style.visibility = &#39;hidden&#39;;
        // alert(&quot;passe 2 bis&quot;);
      }
    }

&lt;!-- language: lang-css --&gt;

    @media (min-width: 800px) {
      .css_menu_1 {
        visibility: visible;
        font-size: 1.3rem;
        background-color: goldenrod;
      }
    }

&lt;!-- language: lang-html --&gt;

    &lt;img src=&quot;../images/hamburger_menu_icon.png&quot; alt=&quot;Display or hide menu 1&quot; class=&quot;css_btn_menu_1&quot; id=&quot;btn_menu_1&quot; onclick=&quot;showOrHideMenu(&#39;menu_1&#39;);&quot;&gt;

&lt;!-- end snippet --&gt;

When I am on a screen which width is less than 800px, I can click on the image.  
The `showOrHideMenu()` function gets executed and indeed the visibility and width of the HTML element which id is `menu_1` are changed as specified.  
I click a second time on the image and the HTML element gets hidden as expected.  
Now, if I increase the size of the viewport, the HTML element is supposed to be visible again.  
Instead of it, I get a blank space.  

Does the execution of the Javascript function conflict with the &quot;execution&quot; of the media query?  
What can I do to fix that?

-- EDIT 1 --

Adding that CSS:

    .css_menu_1.active 
    { 
        visibility : visible; 
        width: 75vw;
    }

And replacing the Javascript by this one:

    &lt;script type=&quot;text/javascript&quot;&gt;
        var btnMenu1 = document.getElementById(&#39;btn_menu_1&#39;);      
        var menu1 = document.getElementById(&#39;menu_1&#39;);       
        btnMenu1.addEventListener(&#39;click&#39;, () =&gt; 
        {
            menu1.classList.toggle(&#39;active&#39;);
        });
    &lt;/script&gt;

solves my problem.

 


</details>


# 答案1
**得分**: 1

当您使用JavaScript更改样式时,它会向元素添加内联样式。内联样式会覆盖任何样式,从而覆盖了在CSS媒体查询中定义的样式。

在这种情况下,您可以在菜单可见时完全删除内联样式。

```javascript
function showOrHideMenu(menuId) {
  var menu = document.getElementById(menuId);

  if (menu.style.visibility === 'hidden' || menu.style.visibility === '') {
    menu.style.visibility = 'visible';
    menu.style.width = '75vw';
  } else {
    menu.style.visibility = '';
    menu.style.width = '';
  }
}

或者您可以添加一个类 "active" 并相应地更改样式:

function showOrHideMenu(menuId) {
  var menu = document.getElementById(menuId);

  if (menu.style.visibility === 'hidden' || menu.style.visibility === '') {
    menu.classList.add('active');
  } else {
    menu.classList.remove('active');
  }
}

在CSS中,您还需要进行以下设置:

.css_menu_1 {
  visibility: hidden;
  width: 0;
} 
.css_menu_1.active { 
  visibility : visible; 
  width: 75vw;
}
英文:

When you are using javascript to change styles, it adds inline style to the element. The inline styles override anything and this leads to overriding your styles defined in CSS media queries.

In this case, you can remove entirely the inline styles when menu is visible.

function showOrHideMenu(menuId) {
  var menu = document.getElementById(menuId);

  if (menu.style.visibility === &#39;hidden&#39; || menu.style.visibility === &#39;&#39;) {
    menu.style.visibility = &#39;visible&#39;;
    menu.style.width = &#39;75vw&#39;;
  } else {
    menu.style.visibility = &#39;&#39;;
    menu.style.width = &#39;&#39;;
  }
}

Or you can add a class " active " and change the styles accordingly :

function showOrHideMenu(menuId) {
  var menu = document.getElementById(menuId);

  if (menu.style.visibility === &#39;hidden&#39; || menu.style.visibility === &#39;&#39;) {
menu.classList.add(&#39;active&#39;);
  } else {
menu.classList.remove(&#39;active&#39;);
  }
}

In css you will also have to do the following :

.css_menu_1 {
  visibility: hidden;
  width: 0;
} 
.css_menu_1.active { 
  visibility : visible; 
  width: 75vw;
}

huangapple
  • 本文由 发表于 2023年6月14日 23:56:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475455.html
匿名

发表评论

匿名网友

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

确定