CSS自定义组合框问题 [英] CSS Custom combobox issue

查看:36
本文介绍了CSS自定义组合框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个自定义组合框.因此,我用 ul 实现了.问题是我无法通过单击 button 在顶部打开组合框列表.在显示 ul 时,它将 button 移至网页底部.

I need a custom combo box. So, I implemented with ul. The problem is I can't get combo box list opens on top by clicking the button. While showing ul, it moves button to bottom of the webpage.

代码:

ul{
    
        width: 100px;
        background-color: rgb(224, 224, 224);
        border-radius: 4px;
        border: 1px solid black;
        padding: 0;
        margin: 0;
    }
    
    ul li{
    
        list-style: none;
        cursor: pointer;
    
        padding: 4px 10px;
    }
    
    li:hover{
    
        background-color: white;
    }
    
    div{
    
        width:  100%;
        height: 40px;
    
        background-color: bisque;
     
    }
    
    section{
    
        height: 400px;
        background-color: #525252;
    }
    
    button{
    
        width: 100px;
        height: 100%;
        margin: 0;
    }

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <section>
            <p>Some content</p>
        </section>
        <div>
            <ul>
                <li>Parrot</li>
                <li>Dog</li>
                <li>Cat</li>
                <li>Squirrel</li>
                <li>Otter</li>
                <li>Cow</li>
                <li>Goat</li>
                <li>Finch</li>
            </ul>
            <button onclick="showPop()">Pet</button>
        </div>
    
        <script>
            let ul = document.getElementsByTagName('ul')[0]
    
            onload = function(){
    
                ul.style.display = 'none'
            }
    
            function showPop(){
    
    
                if(ul.style.display == 'none'){
                    ul.style.display = 'block'
                }else{
                    ul.style.display = 'none'
                }
            }
        </script>
    
    </body>
    </html>

在这里,我想将 ul 保留在 body.div 中.我只想使其弹出,就像带有自定义位置的组合框一样.请参考所附图片.

Here, I want to keep the ul inside the body.div. I just want to make it pop up like combo box with custom position. Please refer attached images.

当前设计:

我希望它是这样的:

如何使用CSS制作它?预先谢谢大家...

How can I make it with CSS? Thank you all in advance...

推荐答案

尝试一下:

ul{

    width: 100px;
    background-color: rgb(224, 224, 224);
    border-radius: 4px;
    border: 1px solid black;
    padding: 0;
    margin: 0;
    position: absolute;
    bottom: 40px;
    z-index: 9;

}

ul li{

    list-style: none;
    cursor: pointer;

    padding: 4px 10px;
}

li:hover{

    background-color: white;
}

div{

    width:  100%;
    height: 40px;

    background-color: bisque;
    
    position: relative;

}

section{

    height: 400px;
    background-color: #525252;
}

button{

    width: 100px;
    height: 100%;
    margin: 0;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <link rel="stylesheet" href="style.css">
</head>
<body>
    <section>
        <p>Some content</p>
    </section>
    <div>
        <ul>
            <li>Parrot</li>
            <li>Dog</li>
            <li>Cat</li>
            <li>Squirrel</li>
            <li>Otter</li>
            <li>Cow</li>
            <li>Goat</li>
            <li>Finch</li>
        </ul>
        <button onclick="showPop()">Pet</button>
    </div>

    <script>
        let ul = document.getElementsByTagName('ul')[0]

        onload = function(){

            ul.style.display = 'none'
        }

        function showPop(){


            if(ul.style.display == 'none'){
                ul.style.display = 'block'
            }else{
                ul.style.display = 'none'
            }
        }
    </script>

</body>
</html>

这篇关于CSS自定义组合框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆