简体   繁体   中英

Jquery - adjust border-radius for a tabbed content

I have the following tabbed content in my site which has border radius around it . The problem is when the first tab is selected the border radius near it still exists and doesn't make it seem good.

在此处输入图片说明

I wrote a piece of jquery to fix it but when it removes the radius, doesn't bring it back when the tab is not selected anymore.

    if (tab_id=="tab-1"){
        $('.tab-content').css("border-top-right-radius","0");
    }
    if (tab_id!=="tab-1"){
        $('.tab-content').css("border-top-right-radius","5px;");
    }

any ideas please??

ps : this is what i'm working on . except i also want the .tab-content to have a border radius of 5pixels.

Well you simply need to create a class

.roundBorder{
  border-top-right-radius:15px;
}

And then simply use addClass() when tab is clicked

$('ul.tabs li').click(function(){
        var tab_id = $(this).attr('data-tab');

        $('ul.tabs li').removeClass('current');
        $('.tab-content').removeClass('current');

        $(this).addClass('current');
        $("#"+tab_id).addClass('current');

        //add this line to add rounded border on tab click           
        $(this).addClass('roundBorder');

    });

});

You're looking for an if statement that checks if the tab selected is the first child. If the statement returns true, then it sets border-radius to 5px on .tab-content on all sides except the top left. If false, then it sets border-radius to 5px on all sides.

This if/else statement needs to be called twice in the code. Once before the .click() function, and one inside it.

 $(document).ready(function() { if ($(".tab-link.current").is(":first-child")) { $(".tab-content").css("border-radius", "0 5px 5px 5px"); } else { $(".tab-content").css("border-radius", "5px"); } $("ul.tabs li").click(function() { var tab_id = $(this).attr("data-tab"); $("ul.tabs li").removeClass("current"); $(".tab-content").removeClass("current"); $(this).addClass("current"); $("#" + tab_id).addClass("current"); if ($(".tab-link.current").is(":first-child")) { $(".tab-content").css("border-radius", "0 5px 5px 5px"); } else { $(".tab-content").css("border-radius", "5px"); } }); }); 
 body { margin-top: 100px; font-family: 'Trebuchet MS', serif; line-height: 1.6; } .container { width: 800px; margin: 0 auto; } ul.tabs { margin: 0px; padding: 0px; list-style: none; } ul.tabs li { background: none; color: #222; display: inline-block; padding: 10px 15px; cursor: pointer; } ul.tabs li.current { background: #ededed; color: #222; border-radius: 5px 5px 0 0; } .tab-content { display: none; background: #ededed; padding: 15px; border-radius: 5px; } .tab-content.current { display: inherit; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <div class="container"> <ul class="tabs"> <li class="tab-link current" data-tab="tab-1">Tab One</li> <li class="tab-link" data-tab="tab-2">Tab Two</li> <li class="tab-link" data-tab="tab-3">Tab Three</li> <li class="tab-link" data-tab="tab-4">Tab Four</li> </ul> <div id="tab-1" class="tab-content current"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </div> <div id="tab-2" class="tab-content"> Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </div> <div id="tab-3" class="tab-content"> Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. </div> <div id="tab-4" class="tab-content"> Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </div> </div> <!-- container --> 

CodePen

did you try like this?

        $('ul.nav li a').filter(function () {
            if (this.id == 'tab_1') {
                $(this).parent().addClass('active');
            }
            else
             {$(this).parent().removeClass('active'); }
        });
    });

And here is active class

 .active{
          border-top-right-radius:10px;
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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