Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
662 views
in Technique[技术] by (71.8m points)

javascript - How to create a button to every list item which opens a menu over it?

I am bulding a mobile responsive web page that looks like the "Play Market" from Android. I have a list of divs which have a button inside them. Each button opens a div with unique links.

how do I create something like this in an efficient way without positioning every div differently?

This is my JSfiddle: http://jsfiddle.net/e0byofe2/

I tried using the position: relative; property to the menu - it made a mess with the elements within the div.

enter image description here

I tried using position: absolute; which is ok, but I can't position the menu over each single div in a global way.

enter image description here

HTML:

<div class="main">

    <div id="header-wrap">
        <div id="header" class="clear">
            <img src="arrow.jpg" />
            <img src="search.jpg" style="float: right;" />
        </div>
    </div>

    <div class="content">
        <div id="apps-header">Apps</div>
        <div class="line"></div>
        <div class="apps">


            <div class="app">
                <img src="app_icon1.jpg" class="app_icon" />
                <div class="app_info">
                    <div class="app_name">text text text</div>
                    <div class="app_comp">text </div>
                    <div class="stars">
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="hstar.png" />
                    </div>  
                    <div class="free">FREE</div>
                </div>
                <div style="float: left;">
                    <img src="3dots.png" class="dots"/>
                </div>
            </div>

            <div class="app">
                <img src="app_icon1.jpg" class="app_icon" />
                <div class="app_info">
                    <div class="app_name">text text text</div>
                    <div class="app_comp">text </div>
                    <div class="stars">
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="hstar.png" />
                    </div>  
                    <div class="free">FREE</div>
                </div>
                <div style="float: left;">
                    <img src="3dots.png" class="dots"/>
                </div>
            </div>

            <div class="app">
                <img src="app_icon1.jpg" class="app_icon" />
                <div class="app_info">
                    <div class="app_name">text text text</div>
                    <div class="app_comp">text </div>
                    <div class="stars">
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="hstar.png" />
                    </div>  
                    <div class="free">FREE</div>
                </div>
                <div style="float: left;">
                    <img src="3dots.png" class="dots"/>
                    <div style="width: 202px; height: 106px; border: 1px solid grey; position: relative; ">

                    </div>
                </div>
            </div>

            <div class="app">
                <img src="app_icon1.jpg" class="app_icon" />
                <div class="app_info">
                    <div class="app_name">text text text</div>
                    <div class="app_comp">text </div>
                    <div class="stars">
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="star.png" />
                        <img src="hstar.png" />
                    </div>  
                    <div class="free">FREE</div>
                </div>
                <div style="float: left;">
                    <img src="3dots.png" class="dots"/>
                </div>
            </div>







        </div>


    </div>






</div>

CSS: body{ margin: 0; height: 100%; }

.main{
    overflow-x: hidden;
    width: 100%;
    height: 100%;
}

#header-wrap {
    width: 100%;
    position: fixed;
    background-color: #689f38;
    height: 62px;
}

#header {
    width: 100%;
    position: absolute;
    bottom: 0;
    box-shadow: 0px 0px 19px rgb(10, 29, 90);
    -webkit-box-shadow: 0px 0px 19px rgb(10, 29, 90);
    -moz-box-shadow: 0px 0px 19px rgb(10, 29, 90);
}


.content{
    padding-top: 80px;
    width: 100%;
    /*height: 100%;*/
    background-color: #eeeeee;
}

#apps-header{
    margin-left: 10px;
    font-family: arial;
    background-image: url('triangle.png');
    background-position: bottom;
    width: 86px;
    background-repeat: no-repeat;
    background-size: 10px;
}

.line{
    width: 100%;
    height: 2px;
    background-color: #458b09;
    margin-top: 10px;
}

.apps{
    width: 100%;
    /*border: 1px solid black;*/
    padding-top: 10px;
    height: 100%;
}

.app{
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    box-shadow: 0px 0px 6px rgb(145, 145, 145);
    -webkit-box-shadow: 0px 0px 6px rgb(145, 145, 145);
    -moz-box-shadow: 0px 0px 6px rgb(145, 145, 145);
    background-color: #fafafa;
    padding: 10px 8px;
    margin: 5px 8px;
    display: inline-table; /******/
}

.app_icon{
    width: 83px;
    height: 83px;
    float: left;
}

.app_info{
    /*border: 1px solid red;*/
    float: left;
    width: 210px;
    height: 81px;
    padding-left: 20px;
}

.app_name{
    font-family: arial;
    font-size: 18px;

}

.app_comp{
    font-family: arial;
    font-size: 16px;    
    color: #595959;
}

.stars img{
    float: left;
    width: 14px; 
    height: 14px;
}

.free{
    color: #69a03a;
    font-family: arial;
    position: relative;
    bottom: -25px;
    left: 105px;
    font-size: 14px;
}

.3dots{
    height: 25px;
}

The best example I can give you is when you open the "Play Market" on your Android and search for a random app, you get a bunch of app div results. When clicking the 3 dots on the top-right corner - it opens a list with 2 items over the specific apps div.

It should look like this:

enter image description here

When you choose an other app and press the 3 dots in the corner, you get the same list with 2 option, but over the apps div.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've changed your structure little bit and made the 'dots' image as a button of the menu with jquery

HTML:

<img src="3dots.png" class="dots"/>
<div class="dots_menu">
    <a href="#">link 1</a>  
    <a href="#">link 2</a>  
</div>

CSS:

.app
{
    position: relative;
}

.dots
{
    float: right;   
}

.dots_menu
{
    display: none;
    width: 202px;
    position: absolute;
    top: 35px;
    right: 0;
    z-index: 1;
    background: rgb(238, 238, 238);
    -webkit-box-shadow: 0px 4px 15px #000;
    -moz-box-shadow: 0px 4px 15px #000;
    box-shadow: 0px 4px 15px #000;
}

.dots_menu.show
{
    display: block;
}

.dots_menu a
{
    display: block;
    text-decoration: none;
    color: #000;
    padding: 10px;
}

JQuery:

$(document).ready(function(){
    $('.dots').click(function(){
        $('.dots_menu').removeClass('show');
        $(this).next().addClass('show');
    });        
});

example: http://jsfiddle.net/e0byofe2/3/

is that what you are looking for?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...