HTML tree full width hover effect

前端 未结 3 1616
醉话见心
醉话见心 2021-01-16 06:55

I have an html tree which is mostly consisting of nested unordered lists.

I need to create a full width hover effect for each leaf, something similar with windows fi

相关标签:
3条回答
  • 2021-01-16 07:34

    A solution changing only the part of the CSS under "hack". I assume that at least you can change that

    CSS

    /*Full width hack*/
    .tree.fullWidth { overflow: hidden; }
    .tree.fullWidth li .item .overlay { right: -8px; width: 314px; left: auto !important; }
    

    demo

    If the tree has a variable width, this is better:

    .tree.fullWidth li .item .overlay { 
        right: -8px; 
        width: 300px;
        padding-left: 14px; 
        left: auto !important; 
    }
    

    It works exactly like the other version, but now the width is the same that the tree width, so the Javascript doesn't need to set a magic width (that you don't know from where it comes:

    0 讨论(0)
  • 2021-01-16 07:50

    I faced a similar situation few days ago.

    Heres 1 way to approach it ( assuming you've got full control over the HTML )

    HTML

    <div class="tree relative">
        <ul>
            <li>
                <div class="item">
                    <div class="overlay"></div>
                    <span class="relative">Node 1</span>
    
                </div>
                <ul>
                    <li>
                        <div class="item selected">
                            <div class="overlay"></div>
                            <span class="relative">Node 2</span>
                        </div>
                    </li>
                </ul>
            </li>
        </ul>
    </div>
    

    CSS

    .relative {
        position:relative;
    }
    
    .tree { 
        width: 300px; 
        border: 1px solid #000; 
        padding: 10px; 
        margin-bottom: 15px; 
    }
    
    .tree ul { margin: 0; padding: 0; list-style-type: none; }
    .tree ul li { padding-left: 16px; }
    
    /*.tree li .item { position: relative; }*/
    
    .tree li .item .overlay { 
        position: absolute; 
        left: 0; 
        right:0;
        /*
        top: 0; 
        width: 100%; 
        height: 100%;
        */
        height:26px; /* well, thats a bummer */
        background-color: #C5E7E6; 
        display: none; 
        border: 1px solid red; 
    }
    .tree li .item:hover .overlay { display: block; }
    

    Fiddle: http://jsfiddle.net/Varinder/5fKP4/2/

    0 讨论(0)
  • 2021-01-16 07:54

    I've had the same problem as you, after some trying and testing I came up with this. It allows you to add scrollbars and put the padding on the inner .

    HTML

    <div class="tree" style="width:256px; height:256px;">
        <div>
            <ul>
                <li>
                    <div><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit</span></div>
                </li>
                <li>
                    <div><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit</span></div>
                    <ul>
                        <li>
                            <div><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit</span></div>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
    </div>
    

    CSS

    .tree {
        position:relative;
        display: block;
        overflow: auto;
        border: 1px solid #8B9097;
        background-color: #fff;
        margin-left:200px;
        /*! margin-left: just for testing */
    }
    .tree > div {
        display:block;
        position:absolute;
        min-width:100%;
    }
    .tree * {
        white-space: nowrap;
    }
    .tree ul {
        margin:0;
        list-style-type: none;
        padding-left: 20px;
    }
    .tree li > div {
        position:relative;
        margin-left:-100000px;
        padding-left:100000px;
        border: 1px solid transparent;
        display:block;
    }
    .tree li div:hover {
        background-color: #B6BABF;
        color: #fff;
    }
    .tree li.collapsed > ul {
        display:none;
    }
    
    .tree li.expanded > ul {
        display:inherit;
    }
    

    http://jsfiddle.net/WknDZ/17/

    0 讨论(0)
提交回复
热议问题