CSS Пользовательские выпадающие списки - PullRequest
2 голосов
/ 02 марта 2020

Мне нужно пользовательское поле со списком. Итак, я реализовал с ul. Проблема в том, что я не могу получить список со списком, который открывается сверху, нажав button. При отображении ul он перемещается button в конец веб-страницы.

Код:

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. Я просто хочу, чтобы он выскочил, как поле со списком с пользовательской позиции. Пожалуйста, обратитесь к приложенным изображениям.

Текущий дизайн:

current design

Я хочу, чтобы это было похоже на

Target design

Как мне сделать это с CSS? Заранее всем спасибо ...

Ответы [ 3 ]

4 голосов
/ 02 марта 2020

Попробуйте это:

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>
2 голосов
/ 02 марта 2020

Попробуйте использовать абсолютную позицию для вашего списка и установить относительную позицию для родительского элемента.

ul {
  width: 100px;
  background-color: rgb(224, 224, 224);
  border-radius: 4px;
  border: 1px solid black;
  padding: 0;
  margin: 0;
  bottom: 40px;
  left: 0;
  position: absolute;
}

ul li {

  list-style: none;
  cursor: pointer;

  padding: 4px 10px;
}

li:hover {

  background-color: white;
}

div {
  width: 100%;
  height: 40px;
  position: relative;
  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>
0 голосов
/ 02 марта 2020

Я изменил ваш CSS немного. Пожалуйста, проверьте фрагмент.

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'
            }
        }
ul{

    width: 100px;
    background-color: rgb(224, 224, 224);
    border-radius: 4px;
    border: 1px solid black;
    padding: 0;
    margin: 0;

  position: absolute;
  bottom: 40px;
}

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;
	position: relative;
  display: inline-block;
}
.dropup{
position: relative;
    display: inline-block;
	}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section>
        <p>Some content</p>
    </section>
    <div class='dropup'>
        <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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...