在垂直制表符效果上获得一个小三角形

时间:2013-11-29 05:09:39

标签: javascript jquery

这是我的 JsFiddle

$(document).ready(function() {
$('.content #tab1, .content #tab2, .content #tab3, .content #tab4').css('display','none');
$('.vertical-tab').click(function() {
    custom_reset();
    $(this).css('background-color','#fff');
    var currentId = $(this).attr('id');
    $('.content').find("#" + currentId).css('display','block');
});
});
function custom_reset() {
$('.vertical-tab').css('background-color','#abcdef');
$('.content #tab1, .content #tab2, .content #tab3, .content #tab4').css('display','none');
}

单击选项卡时,我希望在单击的选项卡的中心显示一个小的左三角形。我怎样才能达到这个效果。

3 个答案:

答案 0 :(得分:1)

<强> CSS

//display the div as a triangle
border-bottom: 10px solid transparent;
border-top: 10px solid transparent;
border-left: 20px solid #abcdef;
height: 0px;
width: 0px;

检查我的更新小提琴http://jsfiddle.net/rynhe/HADnu/12/

<强> JS

$(document).ready(function() {
    $('.content #tab1, .content #tab2, .content #tab3, .content #tab4').css('display','none');
    $('.vertical-tab').click(function() {
        $(".arrow").remove(); /*remove arrow div from a*/
        custom_reset();
        $(this).css('background-color','#fff');
        var currentId = $(this).attr('id');
        var aData = "<div class='arrow'></div>"; 
        $(this).append(aData); /*add arrow div to the current a*/
        $('.content').find("#" + currentId).css('display','block');
    });
});
function custom_reset() {
$('.vertical-tab').css('background-color','#abcdef');
    $('.content #tab1, .content #tab2, .content #tab3, .content #tab4').css('display','none');
}

<强> CSS

ul {
    list-style-type: none;
    height: 408px;
    float: left;
    margin: 0;
    padding: 0;
}
li {

}
ul a {
    width: 20px;
    height: 100px;
    background: #abcdef;
    display: block;
    border: 1px solid #000;
    position:relative; /*Added relative for place the div inside a*/
}

div.arrow {
    border-bottom: 10px solid transparent;
    border-top: 10px solid transparent;
    border-left: 20px solid #abcdef;
    height: 0;
    width: 0px;
    position:absolute;
    top:40px;
    right:0px;
}

答案 1 :(得分:0)

您可以使用

$(this).html("<img class='triangle' src='path/to/triangle.png'>");

继承我的小提琴

http://jsfiddle.net/HADnu/11/

答案 2 :(得分:0)

<li>
  <a href="#" class="vertical-tab" id="tab1"></a>
  <img id="triangle" src="/path"/>
</li>

#triangle{
   position: absolute;
   display:none;
   z-index:1000;
   margin-top:-60px  //as per the position you want to place the image at 
}

$('.vertical-tab').click(function() {

    //your code as it is..

    var currentId = $(this).attr('id');

    $(this).siblings('#triangle').show()  //add this statement

   //your code as it is..

});

/*add #triangle in function custom_reset*/

$('#triangle,.content #tab1, .content #tab2, .content #tab3, .content #tab4').css('display','none');

DEMO