2
我提供了一個JS提琴但它的目標是移動,因此它的工作減少窗口寬度或打開控制檯,但問題是;jQuery自定義手風琴 - 問題關閉當前開放部分
它打開默認選項卡和其內容的罰款,點擊另一個選項卡,它打開並向下滾動。這很好,但是它並沒有關閉前一個選項卡,當我點擊關閉時,會關閉所有打開並使可用性不夠完美。
任何想法的人?有問題的部分,我覺得是這樣的函數中:
var tabClick = function(x) {
而且認爲它我ISOPEN條件是我做錯了
小提琴:https://jsfiddle.net/6wttzcg5/5/
JS:
$(document).ready(function() {
var originalTabs = $('.originalTabs').html();
var windowWidth = 0;
var time = 500;
function clearTabs() {
$('.originalTabs').html(originalTabs);
}
//clearTabs();
//desktopTabs();
function desktopTabs() {
clearTabs();
// cretate tabs for desktop
var headers = $("#tab_description h6");
$('#tab_description h6').each(function(i) {
$(this).nextUntil("h6").andSelf().wrapAll('<div class="tab" id="tab-' + i + '"/>');
});
for (var i = 0; i < headers.length; i++) {
$('.tabs').append('<li class=""><a href="#tab-' + i + '">' + headers[i].innerHTML + '</a></li>');
}
$('ul.tabs').each(function() {
var active, content, links = $(this).find('a');
var listitem = $(this).find('li');
active = listitem.first().addClass('active');
content = $(active.attr('href'));
$('.tab').hide();
$(this).find('a').click(function(e) {
$('.tab').hide();
$('ul.tabs li').removeClass('active');
content.hide();
active = $(this);
content = $($(this).attr('href'));
active.parent().addClass('active');
content.show();
return false;
});
});
headers.remove(); // remove headers from description
$('#tab-0').show(); // show the first tab
}
function mobileTabs() {
clearTabs();
//alert("loaded mobile");
var headers = $("#tab_description h6");
$(headers).each(function(i) {
$(this).append('<a href="#accordion_' + (i + 1) + '" id="accordion_' + (i + 1) + '"></a>');
//$(this).nextUntil("h6").andSelf().wrapAll('<div class="aTab" id="tab-'+i+'"/>');
$(this).on('click tap', function() {
tabClick($(this));
});
});
$('#tab_description h6').first().addClass("active");
$('#tab_description h6').first().nextUntil("h6").show();
}
var tabClick = function(x) {
//alert("clicked");
var accordionContent = $('#tab_description p, #tab_description ul, #tab_description table, #tab_description div');
$('#tab_description h6').removeClass("active");
if (!$(x).hasClass("active")) {
$(x).addClass("active");
}
// Check if current accordion item is open
var isOpen = $(x).next().is(":visible");
//console.log(x);
// Open accordion item if previously closed
if (!isOpen) {
$(x).nextUntil('h6').slideDown(time);
$(x).nextUntil(accordionContent).slideDown(time);
} else {
accordionContent.slideUp(time);
}
scrollToTab($(x));
}
function scrollToTab(tabScrollTo){
$("html, body").animate({ scrollTop: $(tabScrollTo).offset().top - 15 }, time);
}
//load default
$(window).on("load",function(){
if (isMobileLandscapeOnly.matches || isTabletLandscapeOnly.matches) {
//alert("Mobile/Tablet (Portrait)");
desktopTabs();
//$('#tab_description h6').on("click, tap", tabClick);
//console.log(originalTabs);
} else if (isMobilePortraitOnly.matches || isTabletPortraitOnly.matches) {
//alert("Mobile/Tablet (Portrait)");
mobileTabs();
//$('#tab_description h6').on("click, tap", tabClick);
} else if (isDesktop) {
//alert("Desktop");
desktopTabs();
}
});
//bind to resize
$(window).on("orientationchange resize",function(){
if(windowWidth != $(window).width()){
if (isMobileLandscapeOnly.matches || isTabletLandscapeOnly.matches) {
desktopTabs();
$('#tab_description h6').on("click tap", tabClick);
} else if (isMobilePortraitOnly.matches || isTabletPortraitOnly.matches) {
mobileTabs();
$('#tab_description h6').on("click tap", tabClick);
} else if (isDesktop) {
desktopTabs();
}
windowWidth = $(window).width();
delete windowWidth;
}
});
});
任何想法的人?我知道它是一件簡單的事情,但是對我而言,它讓我頭撞在牆上哈哈 – James