窗口调整大小后,按钮应该以不同的方式工作。

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

When the window resizes then the button should work differently

问题

$(document).ready(function(){
    if ($(window).width() < '700') 
    {
        $(".fevent").removeAttr('data-toggle data-target aria-expanded aria-controls');
        $(".fevent").attr({"data-toggle":"collapse", "data-target":"#bol", "aria-expanded":"false", "aria-controls":"bol"});
        $(".fevent").on("click",function(){
            $("#bol").collapse('toggle');
        });
    } else if ($(window).width() > '700') 
    {
        $(".fevent").removeAttr('data-toggle data-target aria-expanded aria-controls');
        $(".fevent").attr({"data-toggle":"collapse", "data-target":"#col", "aria-expanded":"false", "aria-controls":"col"});
        $(".fevent").on("click", function(){
            $("#col").collapse('toggle');
        });
    }
});

.fvent 是按钮的类,当我调整窗口大小时,按钮应该以不同的方式工作,当窗口小于 700 时,单击按钮会显示 div id #bol,否则显示 #col。

按钮在宽度大于 700 时正常工作,但当我将窗口大小调整为小于 700 时,它仍然按照宽度大于 700 的方式工作。希望这对你有所帮助!

英文:
$(document).ready(function(){
if ($(window).width &lt; &#39;700&#39;) 
{
	$(&quot;.fevent&quot;).removeAttr(&#39;data-toggle data-target aria-expanded aria-controls&#39;);
	$(&quot;.fevent&quot;).attr({&quot;data-toggle&quot;:&quot;collapse&quot;, &quot;data-target&quot;:&quot;#bol&quot;, &quot;aria-expanded&quot;:&quot;false&quot;, &quot;aria-controls&quot;:&quot;bol&quot;});
	$(&quot;.fevent&quot;).on(&quot;click&quot;,function(){
    $(&quot;#bol&quot;).collapse(&#39;toggle&#39;);
	});
} else if ($(window).width &gt; &#39;700&#39;) 
{
	$(&quot;.fevent&quot;).removeAttr(&#39;data-toggle data-target aria-expanded aria-controls&#39;);
	$(&quot;.fevent&quot;).attr({&quot;data-toggle&quot;:&quot;collapse&quot;, &quot;data-target&quot;:&quot;#col&quot;, &quot;aria-expanded&quot;:&quot;false&quot;, &quot;aria-controls&quot;:&quot;col&quot;});
	$(&quot;.fevent&quot;).on(&quot;click&quot;, function(){
    $(&quot;#col&quot;).collapse(&#39;toggle&#39;);
	});
}
});

.fvent is the class of button , when i resize my window then the button should function differently,
when the window is less than the 700 then clicking on button show div id #bol otherwise #col .

The button is working for width > 700 when i resizes the window less than 700 it still works as the width > 700.

PLEASE HELP ME OUT!!!

答案1

得分: 5

这是您提供的HTML和CSS代码,用于根据媒体查询显示和隐藏两个按钮。如果您需要进一步的翻译或有其他问题,请随时提出。

英文:

You do not need to use any additional JavaScript for this. Just use 2 buttons and show and hide them with media queries:

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

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

.btn.col {
  display:none;
}

@media (min-width: 700px) {
  .btn.col {
    display:inline-block;
  }
  .btn.bol {
    display:none;
  }
}

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

&lt;link href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot;/&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;button class=&quot;btn btn-primary bol&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#bol&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;bol&quot;&gt;Toggle bol&lt;/button&gt;
&lt;button class=&quot;btn btn-primary col&quot; type=&quot;button&quot; data-toggle=&quot;collapse&quot; data-target=&quot;#col&quot; aria-expanded=&quot;false&quot; aria-controls=&quot;col&quot;&gt;Toggle col&lt;/button&gt;
&lt;div class=&quot;row&quot;&gt;
  &lt;div class=&quot;col&quot;&gt;
    &lt;div class=&quot;collapse&quot; id=&quot;col&quot;&gt;
      &lt;div class=&quot;card card-body&quot;&gt;
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec venenatis enim ornare, ullamcorper ipsum ut, bibendum mi. Quisque sit amet velit dignissim, tincidunt neque nec, consequat dolor.
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;row&quot;&gt;
  &lt;div class=&quot;col&quot;&gt;
    &lt;div class=&quot;collapse&quot; id=&quot;bol&quot;&gt;
      &lt;div class=&quot;card card-body&quot;&gt;
        Vivamus auctor commodo nisl ut vestibulum. Aliquam erat volutpat. Aliquam eu leo non nunc ullamcorper fermentum. Donec vehicula dolor sed augue maximus, non congue urna semper.
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

$(document).ready()只会在文档加载时触发一次,所以您只在文档加载时检查视口宽度。

您需要添加$(window).on('resize')函数,并在那里检查视口大小。

英文:

The $(document).ready() is trigged only once so you are checking viewport width only on document load.

You have to add $(window).on(&#39;resize&#39;) function and check viewport the size there.

答案3

得分: 1

$(document).ready() 仅在页面加载时触发;它不会在页面调整大小时触发。

您可以尝试 $(window).on('resize', function(){ ...

英文:

$(document).ready() is triggered only the page loads; it's not triggered when the page is resized.

You can try $(window).on(&#39;resize&#39;,function(){ ...

huangapple
  • 本文由 发表于 2020年1月7日 00:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615855.html
匿名

发表评论

匿名网友

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

确定