英文:
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 < '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 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 -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<button class="btn btn-primary bol" type="button" data-toggle="collapse" data-target="#bol" aria-expanded="false" aria-controls="bol">Toggle bol</button>
<button class="btn btn-primary col" type="button" data-toggle="collapse" data-target="#col" aria-expanded="false" aria-controls="col">Toggle col</button>
<div class="row">
<div class="col">
<div class="collapse" id="col">
<div class="card card-body">
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.
</div>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="collapse" id="bol">
<div class="card card-body">
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.
</div>
</div>
</div>
</div>
<!-- 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('resize')
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('resize',function(){ ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论