2016-05-03 92 views
0

目前我正在爲我公司開發一個Web應用程序,並且最近開始學習JS/Jquery。我已經成立了一個標籤插件/ libary內容從下面GitHub的鏈接:根據分辨率更改JS功能

https://github.com/samsono/Easy-Responsive-Tabs-to-Accordion

JS文件

// Easy Responsive Tabs Plugin 
// Author: Samson.Onna <Email : [email protected]> 
(function ($) { 
    $.fn.extend({ 
     easyResponsiveTabs: function (options) { 
      //Set the default values, use comma to separate the settings, example: 
      var defaults = { 
       type: 'default', //default, vertical, accordion; 
       width: 'auto', 
       fit: true, 
       closed: false, 
       tabidentify: '', 

       activate: function() { 
       } 
      } 
      //Variables 
      var options = $.extend(defaults, options); 
      var opt = options, jtype = opt.type, jfit = opt.fit, jwidth = opt.width, vtabs = 'vertical', accord = 'accordion'; 
      var hash = window.location.hash; 
      var historyApi = !!(window.history && history.replaceState ); 

      //Events 
      $(this).bind('tabactivate', function (e, currentTab) { 
       if (typeof options.activate === 'function') { 
        options.activate.call(currentTab, e) 
       } 
      }); 

      //Main function 
      this.each(function() { 
       var $respTabs = $(this); 
       var $respTabsList = $respTabs.find('ul.resp-tabs-list.' + options.tabidentify); 
       var respTabsId = $respTabs.attr('id'); 
       $respTabs.find('ul.resp-tabs-list.' + options.tabidentify + ' li').addClass('resp-tab-item').addClass(options.tabidentify); 
       $respTabs.css({ 
        'display': 'block', 
        'width': jwidth 
       }); 

       if (options.type == 'vertical') 
        $respTabsList.css('margin-top', '0px'); 

       $respTabs.find('.resp-tabs-container.' + options.tabidentify).css('border-color', options.active_content_border_color); 
       $respTabs.find('.resp-tabs-container.' + options.tabidentify + ' > div').addClass('resp-tab-content').addClass(options.tabidentify); 
       jtab_options(); 
       //Properties Function 
       function jtab_options() { 
        if (jtype == vtabs) { 
         $respTabs.addClass('resp-vtabs').addClass(options.tabidentify); 
        } 
        if (jfit == true) { 
         $respTabs.css({ width: '100%', margin: '0px' }); 
        } 
        if (jtype == accord) { 
         $respTabs.addClass('resp-easy-accordion').addClass(options.tabidentify); 
         $respTabs.find('.resp-tabs-list').css('display', 'none'); 
        } 
       } 

       //Assigning the h2 markup to accordion title 
       var $tabItemh2; 
       $respTabs.find('.resp-tab-content.' + options.tabidentify).before("<h2 class='resp-accordion " + options.tabidentify + "' role='tab'><span class='resp-arrow'></span></h2>"); 

       $respTabs.find('.resp-tab-content.' + options.tabidentify).prev("h2").css({ 
        'background-color': options.inactive_bg, 
        'border-color': options.active_border_color 
       }); 

       var itemCount = 0; 
       $respTabs.find('.resp-accordion').each(function() { 
        $tabItemh2 = $(this); 
        var $tabItem = $respTabs.find('.resp-tab-item:eq(' + itemCount + ')'); 
        var $accItem = $respTabs.find('.resp-accordion:eq(' + itemCount + ')'); 
        $accItem.append($tabItem.html()); 
        $accItem.data($tabItem.data()); 
        $tabItemh2.attr('aria-controls', options.tabidentify + '_tab_item-' + (itemCount)); 
        itemCount++; 
       }); 

       //Assigning the 'aria-controls' to Tab items 
       var count = 0, 
        $tabContent; 
       $respTabs.find('.resp-tab-item').each(function() { 
        $tabItem = $(this); 
        $tabItem.attr('aria-controls', options.tabidentify + '_tab_item-' + (count)); 
        $tabItem.attr('role', 'tab'); 
        $tabItem.css({ 
         'background-color': options.inactive_bg, 
         'border-color': 'none' 
        }); 

        //Assigning the 'aria-labelledby' attr to tab-content 
        var tabcount = 0; 
        $respTabs.find('.resp-tab-content.' + options.tabidentify).each(function() { 
         $tabContent = $(this); 
         $tabContent.attr('aria-labelledby', options.tabidentify + '_tab_item-' + (tabcount)).css({ 
          'border-color': options.active_border_color 
         }); 
         tabcount++; 
        }); 
        count++; 
       }); 

       // Show correct content area 
       var tabNum = 0; 
       if (hash != '') { 
        var matches = hash.match(new RegExp(respTabsId + "([0-9]+)")); 
        if (matches !== null && matches.length === 2) { 
         tabNum = parseInt(matches[1], 10) - 1; 
         if (tabNum > count) { 
          tabNum = 0; 
         } 
        } 
       } 

       //Active correct tab 
       $($respTabs.find('.resp-tab-item.' + options.tabidentify)[tabNum]).addClass('resp-tab-active').css({ 
        'background-color': options.activetab_bg, 
        'border-color': options.active_border_color 
       }); 

       //keep closed if option = 'closed' or option is 'accordion' and the element is in accordion mode 
       if (options.closed !== true && !(options.closed === 'accordion' && !$respTabsList.is(':visible')) && !(options.closed === 'tabs' && $respTabsList.is(':visible'))) { 
        $($respTabs.find('.resp-accordion.' + options.tabidentify)[tabNum]).addClass('resp-tab-active').css({ 
         'background-color': options.activetab_bg + ' !important', 
         'border-color': options.active_border_color, 
         'background': 'none' 
        }); 

        $($respTabs.find('.resp-tab-content.' + options.tabidentify)[tabNum]).addClass('resp-tab-content-active').addClass(options.tabidentify).attr('style', 'display:block'); 
       } 
       //assign proper classes for when tabs mode is activated before making a selection in accordion mode 
       else { 
        // $($respTabs.find('.resp-tab-content.' + options.tabidentify)[tabNum]).addClass('resp-accordion-closed'); //removed resp-tab-content-active 
       } 

       //Tab Click action function 
       $respTabs.find("[role=tab]").each(function() { 

        var $currentTab = $(this); 
        $currentTab.hover(function() { 

         var $currentTab = $(this); 
         var $tabAria = $currentTab.attr('aria-controls'); 

         if ($currentTab.hasClass('resp-accordion') && $currentTab.hasClass('resp-tab-active')) { 
          $respTabs.find('.resp-tab-content-active.' + options.tabidentify).slideUp('', function() { 
           $(this).addClass('resp-accordion-closed'); 
          }); 
          $currentTab.removeClass('resp-tab-active').css({ 
           'background-color': options.inactive_bg, 
           'border-color': 'none' 
          }); 
          return false; 
         } 
         if (!$currentTab.hasClass('resp-tab-active') && $currentTab.hasClass('resp-accordion')) { 
          $respTabs.find('.resp-tab-active.' + options.tabidentify).removeClass('resp-tab-active').css({ 
           'background-color': options.inactive_bg, 
           'border-color': 'none' 
          }); 
          $respTabs.find('.resp-tab-content-active.' + options.tabidentify).slideUp().removeClass('resp-tab-content-active resp-accordion-closed'); 
          $respTabs.find("[aria-controls=" + $tabAria + "]").addClass('resp-tab-active').css({ 
           'background-color': options.activetab_bg, 
           'border-color': options.active_border_color 
          }); 

          $respTabs.find('.resp-tab-content[aria-labelledby = ' + $tabAria + '].' + options.tabidentify).slideDown().addClass('resp-tab-content-active'); 
         } else { 
          console.log('here'); 
          $respTabs.find('.resp-tab-active.' + options.tabidentify).removeClass('resp-tab-active').css({ 
           'background-color': options.inactive_bg, 
           'border-color': 'none' 
          }); 

          $respTabs.find('.resp-tab-content-active.' + options.tabidentify).removeAttr('style').removeClass('resp-tab-content-active').removeClass('resp-accordion-closed'); 

          $respTabs.find("[aria-controls=" + $tabAria + "]").addClass('resp-tab-active').css({ 
           'background-color': options.activetab_bg, 
           'border-color': options.active_border_color 
          }); 

          $respTabs.find('.resp-tab-content[aria-labelledby = ' + $tabAria + '].' + options.tabidentify).addClass('resp-tab-content-active').attr('style', 'display:block'); 
         } 
         //Trigger tab activation event 
         $currentTab.trigger('tabactivate', $currentTab); 

         //Update Browser History 
         if (historyApi) { 
          var currentHash = window.location.hash; 
          var tabAriaParts = $tabAria.split('tab_item-'); 
          // var newHash = respTabsId + (parseInt($tabAria.substring(9), 10) + 1).toString(); 
          var newHash = respTabsId + (parseInt(tabAriaParts[1], 10) + 1).toString(); 
          if (currentHash != "") { 
           var re = new RegExp(respTabsId + "[0-9]+"); 
           if (currentHash.match(re) != null) { 
            newHash = currentHash.replace(re, newHash); 
           } 
           else { 
            newHash = currentHash + "|" + newHash; 
           } 
          } 
          else { 
           newHash = '#' + newHash; 
          } 

          history.replaceState(null, null, newHash); 
         } 
        }); 

       }); 

       //Window resize function 
       $(window).resize(function() { 

        $respTabs.find('.resp-accordion-closed').removeAttr('style'); 

       }); 
      }); 
     } 
    }); 
})(jQuery); 

Orginally選項卡設置爲上線人數142這樣的的onclick :

$currentTab.click(function() { 

我已經改變了選項卡,懸停,因爲我喜歡,更多的我的應用程序,就像這樣:

$currentTab.hover(function() { 

它可以在我的桌面上正常工作,但是當我將瀏覽器調整爲較小的分辨率時,選項卡將在手風琴中更改。通常它工作得很好,但因爲我已經將它改爲懸停系統,所以現在不是最好的選擇。有沒有什麼辦法可以在javascript/jquery中檢測到較小的分辨率,所以當它在800px下的功能變回$currentTab.click(function() {?或者我的問題的任何其他解決方案?我想要:根據分辨率更改jquery懸停事件函數或基於分辨率更改JS腳本(所以我創建了2個js文件,一個用於移動設備,另一個用於桌面,然後決定我將加載哪個腳本)。

回答

0

將名爲功能,做

if (window.screen.width < whatever) $currentTab.click(doIt); 
else $currentTab.hover(doIt);