简体   繁体   English

Javascript标签-如何在URL上添加井号?

[英]Javascript Tabs - How to add hashtags on URL?

My Tabs works fine, but I need to have a separate function which directs with a URL to the tab, so I can have a separate link for each tab. 我的标签页工作正常,但是我需要一个单独的功能,该功能将URL定向到该标签页,因此我可以为每个标签页设置单独的链接。

How Can I achieve that? 我该如何实现?

url.com/#tab1 url.com/#tab1

url.com/#tab2 url.com/#tab2

I tried many ways but I keep getting undefined hashtag. 我尝试了很多方法,但一直得到未定义的标签。 I'd like advice please, I put together a demo, which shows the full working experience. 我需要建议,我整理了一个演示,展示了完整的工作经验。

 var boxLink = $( '.but' ); var box = $( '.tab' ); boxLink.click( function() { $('#but1').removeClass('active'); var boxID = $(this).attr("data-target"); var currentbox = $('.tab:not(.is-hidden)'); var targetBox = $('.tab#' + boxID); if (!$(this).hasClass('active')) { boxLink.removeClass('active'); $(this).addClass('active'); TweenMax.to( currentbox, 0.2, {ease:Power4.easeOut, className: '-=visible', autoAlpha: 0, onComplete: boxIn, onCompleteParams: [targetBox] }); } return false; }); var boxIn = function( targetBox ) { box.addClass( 'is-hidden' ); targetBox.removeClass( 'is-hidden' ); TweenMax.to( targetBox, 0.2, {autoAlpha: 1,className: '+=visible', ease:Power4.easeIn}); } 
 body { background-color:black; } #tab01 { background: white; } #tab02 { background: red; } .tab { width:100px; height:100px; font-size:30px; line-height:100px; text-align:center; } .is-hidden { display: none; opacity:0; visibility:hidden; } button { cursor:pointer; padding: 5px; margin-top: 10px; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TweenMax.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="tab" id="tab01">one</div> <div class="tab is-hidden" id="tab02">two</div> <button class="but active" id="but1" data-target="tab01">slide 1</button> <button class="but" id="but2" data-target="tab02">slide 2</button> 

If I got you correctly, you want to show a specific tab on load in case a hash has been added to the page's URL, right? 如果我理解正确,您希望在加载时显示一个特定的选项卡,以防万一哈希已添加到页面的URL中,对吗? If so you should parse window.location.href and check for a hash tag in the URL. 如果是这样,则应解析window.location.href并检查URL中的哈希标记。 If it is present, the hash value eg '#tab2' should show the container with the same ID. 如果存在,则哈希值(例如“#tab2”)应显示具有相同ID的容器。

Updated code: 更新的代码:

var currentUrl= window.location.href;
if (currentUrl.indexOf('#') > -1) {
  var hashTag= currentUrl.substring(currentUrl.indexOf('#')+1);
  var targetBox = $('.tab#' + hashTag);
  TweenMax.to( currentbox, 0.2, {ease:Power4.easeOut, 
    className: '-=visible', autoAlpha: 0,  onComplete: boxIn, onCompleteParams: [targetBox] });

  // added: highlight the proper buttons and remove the default "active" element
  $('.active').removeClass('active');
  $('.' + hashTag + '-control').addClass('active');
}

To also highlight the buttons, you need to give them a class (that's the easiest method as you can't use duplicate IDs so we identify the tab via ID and button via class): 为了突出显示按钮,您需要给它们提供一个类(这是最简单的方法,因为您不能使用重复的ID,所以我们通过ID来标识选项卡,并通过类来标识按钮):

<button  class="tab01-control but active" id="but1" data-target="tab01">slide 1</button>
<button  class="tab02-control but" id="but2" data-target="tab02">slide 2</button>

Your boxIn(targetBox) method expects to receive a jQuery object as "targetBox" parameter and it was a string. 您的boxIn(targetBox)方法希望接收一个jQuery对象作为“ targetBox”参数,它是一个字符串。 Simply append "#tab02" to your URL when you test it locally to have the second tab show up. 在本地测试“#tab02”时,只需将其附加到您的URL即可显示第二个选项卡。

Side note: 边注:

Your Code snippet is currently not working correctly, you should include ... 您的代码段当前无法正常工作,您应该添加...

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.3/TweenMax.min.js"></script>

... to make your animation work as expected, otherwise you'll receive a JS error message when clicking one of the buttons. ...以使您的动画按预期工作,否则,当单击其中一个按钮时,您将收到JS错误消息。

Edit 1 编辑1

As you asked how to use meaningful "hash names" instead of the control's IDs like tab01 , this can be achieved with a simple switch-case: 当您询问如何使用有意义的“哈希名称”而不是控件的ID(如tab01 ,可以通过一个简单的切换方式来实现:

let hashTag = window.location.hash;
if (hashTag !== '') {
    hashTag = hashTag.substring(1);

    let targetName = '';

    switch (hashTag) {
        case "home":
            targetName = 'tab01';
            break;
        case "profile":
            targetName = 'tab02';
            break;
    }
    if (targetName !== '') {
        var targetBox = $('.tab#' + targetName);
        ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM