我的頁腳中有這個手風琴,只能在移動設備上運行。當我最初在桌面上運行它時,該功能不會運行,這就是我想要的。但是,如果我切換到移動視圖然后回傳桌面,該功能仍然有效......我仍然可以單擊 h5 并顯示/隱藏下面的文本......但這不應該發生在桌面上,沒看懂問題。。
Codepen,因此您可以看到調整大小的問題。
// On resize run the check screen size function
$(window).on("resize", function (e) {
checkScreenSize();
});
// Run the check screen size function on load
checkScreenSize();
// check screen size function
function checkScreenSize(){
var newWindowWidth = $(window).width();
// Run the accordion function on screens less than 1024
if (newWindowWidth < 1024) {
footerAccordion();
}
}
// Accordion function
function footerAccordion() {
$('.footer__locations p').hide();
$(".footer__locations").find("h5").click(function() {
$(".footer__locations").find("h5").removeClass('active');
$('.footer__locations p').slideUp();
var selected = $(this).next('.footer__locations p');
if (selected.is(":hidden")) {
$(this).next('.footer__locations p').slideDown();
$(this).toggleClass('active');
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<footer class="footer">
<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
<div class="row footer__locations-row">
<div class="col-lg-6 footer__locations">
<h5>TITLE TWO</h5>
<p>THIS IS SOME TEXT TWO</p>
</div>
<div class="col-lg-6 footer__locations">
<h5>TITLE TWO</h5>
<p>THIS IS SOME TEXT TWO</p>
</div>
</div>
</div>
</div>
</div>
</footer>
uj5u.com熱心網友回復:
問題是您分配了 Click 操作并且從不洗掉它。一種解決方法就是接受這一點 - 將您的移動檢查放在點擊處理程式中。然后使用 CSS 來處理 p 元素的隱藏/顯示。
function IsMobileScreenSize(){
var newWindowWidth = $(window).width();
// true if screens less than 1024
if (newWindowWidth < 1024) {
return true;
}
return false;
}
// $('.footer__locations p').hide(); // Handle UI in CSS instead
// Accordion function just set it always
$(".footer__locations").find("h5").click(function() {
// Only do for larger screen sizes, otherwise just stop here
if ( ! IsMobileScreenSize() )
return;
$(".footer__locations").find("h5").removeClass('active');
$('.footer__locations p').slideUp();
var selected = $(this).next('.footer__locations p');
if (selected.is(":hidden")) {
$(this).next('.footer__locations p').slideDown();
$(this).toggleClass('active');
}
});
最后,您的 checkScreenSize 函式將如下所示
function checkScreenSize(){
if (IsMobileScreenSize()) {
$('.footer__locations p').hide();
} else {
$('.footer__locations p').show();
}
}
uj5u.com熱心網友回復:
做一個反向手風琴功能和
function reverseFooterAccordion() {
$('.footer__locations p').show();
$(".footer__locations").find("h5").off("click.namespace") // use namespaces to on and off click handler
}
在其他條件下呼叫它
if (newWindowWidth < 1024) {
footerAccordion();
} else reverseFooterAccordion()
正確實施的 footerAccordion 看起來像
function footerAccordion() {
$('.footer__locations p').hide();
$(".footer__locations").find("h5").off("click.namespace").on("click.namespace",function() {
$(".footer__locations").find("h5").removeClass('active');
$('.footer__locations p').slideUp();
var selected = $(this).next('.footer__locations p');
if (selected.is(":hidden")) {
$(this).next('.footer__locations p').slideDown();
$(this).toggleClass('active');
}
});
}
PS:在調整大小偵聽器上添加去抖動以提高性能
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418586.html
標籤:
