P
P
Pavel_Proca2016-11-18 18:58:04
css
Pavel_Proca, 2016-11-18 18:58:04

How to make dropdown show on click?

How can I change the styles so that the menu is shown when clicking on the dropdown, and not when hovering?

<div class="wrapper">

    <div class="dropdown">
      <label data-value="">Select an option...</label>
      <ul>
        <li data-value="1">Option #1</li>
        <li data-value="2">Option #2</li>
        <li data-value="3">Option #3</li>
      </ul>
    </div>

  </div>

* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
}

html, body {
  background: #343838;
  font: 18px 'Avant Garde', Avantgarde, 'Century Gothic', CenturyGothic, AppleGothic, sans-serif;
  line-height: 18px;
  border: none;
  width: 100%;
  height: 100%;
}

.wrapper {
  display: flex;
  align-items: center;
  width: 100%;
  height: 100%;
}

.dropdown {
  margin: 0px auto;
  position: relative;
  width: calc(100% - 40px);
  max-width: 400px;
}

.dropdown label {
  display: block;
  padding: 8px 16px;
  background: #EA8C02;
  color: #FAFAFA;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
  cursor: pointer;
}

.dropdown ul {
  list-style: none;
  position: absolute;
  width: 100%;
}

.dropdown ul li {
  padding: 0px 16px;
  background: #DDD;
  color: #343838;
  text-shadow: 1px 1px 1px rgba(255, 255, 255, 0.8);
  height: 0px;
  overflow: hidden;
  cursor: pointer;
  transition: height 0.1s linear 0s;
}

.dropdown ul li:hover,
.dropdown ul li.selected {
  background: #00B4CC;
  color: #FAFAFA;
  border-left: 2px solid blue !important;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
}

.dropdown:hover ul li {
  padding: 8px 16px;
  height: 34px;
  border: 1px solid gray;
  border-top: none;
}

$(function() {
  $('.dropdown ul li').on('click', function() {
    var label = $(this).parent().parent().children('label');
    label.attr('data-value', $(this).attr('data-value'));
    label.html($(this).html());

    $(this).parent().children('.selected').removeClass('selected');
    $(this).addClass('selected');
  });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iBird Rose, 2016-11-18
@Pavel_Proca

https://jsfiddle.net/sbr2wx1w/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question