将手风琴转换为垂直显示?

时间:2017-02-27 14:22:34

标签: html css

我不知道这是否可行,但我想知道是否可以简单地将垂直手风琴转换为普通水平导航或者保持原始CSS和html。

我只想让它水平显示而不是垂直显示。尝试浮动一些元素并为每个元素创建一个div,但可以使其工作。

任何帮助将不胜感激。谢谢。

$(function() {
var Accordion = function(el, multiple) {
this.el = el || {};
this.multiple = multiple || false;


var links = this.el.find('.link');
// Event
links.on('click', {el: this.el, multiple: this.multiple}, this.dropdown)
}

Accordion.prototype.dropdown = function(e) {
var $el = e.data.el;
$this = $(this),
$next = $this.next();

$next.slideToggle();
$this.parent().toggleClass('open');

if (!e.data.multiple) {
$el.find('.submenu').not($next).slideUp().parent().removeClass('open');
};
}

var accordion = new Accordion($('#accordion'), false);
});
ul {
list-style-type: none;
}

a {
color: #b63b4d;
text-decoration: none;
}


h1 {
color: #FFF;
font-size: 24px;
font-weight: 400;
text-align: center;
}

 

h1 a {
color: #c12c42;
font-size: 16px;
}

.accordion {
width: 100%;
max-width: 300px;
background: #FFF;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;

margin-right: 100px;
}

.accordion .link {
cursor: pointer;
display: block;
padding: 15px 15px 15px 42px;
color: #4D4D4D;
font-size: 14px;
font-weight: 700;
border-bottom: 1px solid #CCC;
position: relative;
-webkit-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
transition: all 0.4s ease;
}

.accordion li:last-child .link {
border-bottom: 0;
}

.accordion li i {
position: absolute;

float: left;
top: 16px;
left: 12px;
font-size: 18px;
color: #595959;
-webkit-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
transition: all 0.4s ease;
}

.accordion li i.fa-chevron-down {
right: 12px;
left: auto;
font-size: 16px;
}

.accordion li.open .link {
color: #b63b4d;
}

.accordion li.open i {
color: #b63b4d;
}
.accordion li.open i.fa-chevron-down {
-webkit-transform: rotate(180deg);
-ms-transform: rotate(180deg);
-o-transform: rotate(180deg);
transform: rotate(180deg);
}

.accordion li.default .submenu {display: block;}


/**
* Submenu
-----------------------------*/
.submenu {
display: none;
background: #444359;
font-size: 14px;
}

.submenu li {
border-bottom: 1px solid #4b4a5e;
}

.submenu a {
display: block;
text-decoration: none;
color: #d9d9d9;
padding: 12px;
padding-left: 42px;
-webkit-transition: all 0.25s ease;
-o-transition: all 0.25s ease;
transition: all 0.25s ease;
}

.submenu a:hover {
background: #b63b4d;
color: #FFF;
}
<ul id="accordion" class="accordion">
<li>
<div class="link"><i class="fa fa-paint-brush"></i>Home<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li><a href="#">Photoshop</a></li>
<li><a href="#">HTML</a></li>
<li><a href="#">CSS</a></li>
<li><a href="#">Test</a></li>
</ul>
</li>
<li class="default open">
<div class="link"><i class="fa fa-code"></i>Profile<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li><a href="#">Javascript</a></li>
<li><a href="#">jQuery</a></li>
<li><a href="#">Frameworks javascript</a></li>
</ul>
</li>
<li>
<div class="link"><i class="fa fa-mobile"></i>History<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li><a href="#">Tablets</a></li>
<li><a href="#">Mobile</a></li>
<li><a href="#">Desktops/a></li>
<li><a href="#">Test</a></li>

</ul>
</li>
<li><div class="link"><i class="fa fa-globe"></i>External Sites<i class="fa fa-chevron-down"></i></div>
<ul class="submenu">
<li><a href="#">Google</a></li>
<li><a href="#">Bing</a></li>
<li><a href="#">Yahoo</a></li>
<li><a href="#">GMAIL</a></li>

</ul>
</li>
</ul>

 

1 个答案:

答案 0 :(得分:2)

您只需从父级移除max-width并将.accordion > li置于内联中:

.accordion {
    max-width: 100%;
}
.accordion > li {
    display: inline-block;
    vertical-align: top;
}

$(function() {
  var Accordion = function(el, multiple) {
    this.el = el || {};
    this.multiple = multiple || false;


    var links = this.el.find('.link');
    // Event
    links.on('click', {
      el: this.el,
      multiple: this.multiple
    }, this.dropdown)
  }

  Accordion.prototype.dropdown = function(e) {
    var $el = e.data.el;
    $this = $(this),
      $next = $this.next();

    $next.slideToggle();
    $this.parent().toggleClass('open');

    if (!e.data.multiple) {
      $el.find('.submenu').not($next).slideUp().parent().removeClass('open');
    };
  }

  var accordion = new Accordion($('#accordion'), false);
});
ul {
  list-style-type: none;
}

a {
  color: #b63b4d;
  text-decoration: none;
}

h1 {
  color: #FFF;
  font-size: 24px;
  font-weight: 400;
  text-align: center;
}

h1 a {
  color: #c12c42;
  font-size: 16px;
}

.accordion {
  width: 100%;
  max-width: 300px;
  background: #FFF;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  margin-right: 100px;
}

.accordion .link {
  cursor: pointer;
  display: block;
  padding: 15px 15px 15px 42px;
  color: #4D4D4D;
  font-size: 14px;
  font-weight: 700;
  border-bottom: 1px solid #CCC;
  position: relative;
  -webkit-transition: all 0.4s ease;
  -o-transition: all 0.4s ease;
  transition: all 0.4s ease;
}

.accordion li:last-child .link {
  border-bottom: 0;
}

.accordion li i {
  position: absolute;
  float: left;
  top: 16px;
  left: 12px;
  font-size: 18px;
  color: #595959;
  -webkit-transition: all 0.4s ease;
  -o-transition: all 0.4s ease;
  transition: all 0.4s ease;
}

.accordion li i.fa-chevron-down {
  right: 12px;
  left: auto;
  font-size: 16px;
}

.accordion li.open .link {
  color: #b63b4d;
}

.accordion li.open i {
  color: #b63b4d;
}

.accordion li.open i.fa-chevron-down {
  -webkit-transform: rotate(180deg);
  -ms-transform: rotate(180deg);
  -o-transform: rotate(180deg);
  transform: rotate(180deg);
}

.accordion li.default .submenu {
  display: block;
}


/**
* Submenu
-----------------------------*/

.submenu {
  display: none;
  background: #444359;
  font-size: 14px;
}

.submenu li {
  border-bottom: 1px solid #4b4a5e;
}

.submenu a {
  display: block;
  text-decoration: none;
  color: #d9d9d9;
  padding: 12px;
  padding-left: 42px;
  -webkit-transition: all 0.25s ease;
  -o-transition: all 0.25s ease;
  transition: all 0.25s ease;
}

.submenu a:hover {
  background: #b63b4d;
  color: #FFF;
}

/** ADDED CODE */
.accordion {
  max-width: 100%;
}
.accordion > li {
 display: inline-block;
 vertical-align: top;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="accordion" class="accordion">
  <li>
    <div class="link"><i class="fa fa-paint-brush"></i>Home<i class="fa fa-chevron-down"></i></div>
    <ul class="submenu">
      <li><a href="#">Photoshop</a></li>
      <li><a href="#">HTML</a></li>
      <li><a href="#">CSS</a></li>
      <li><a href="#">Test</a></li>
    </ul>
  </li>
  <li class="default open">
    <div class="link"><i class="fa fa-code"></i>Profile<i class="fa fa-chevron-down"></i></div>
    <ul class="submenu">
      <li><a href="#">Javascript</a></li>
      <li><a href="#">jQuery</a></li>
      <li><a href="#">Frameworks javascript</a></li>
    </ul>
  </li>
  <li>
    <div class="link"><i class="fa fa-mobile"></i>History<i class="fa fa-chevron-down"></i></div>
    <ul class="submenu">
      <li><a href="#">Tablets</a></li>
      <li><a href="#">Mobile</a></li>
      <li><a href="#">Desktops/a></li>
<li><a href="#">Test</a></li>

    </ul>
  </li>
  <li>
    <div class="link"><i class="fa fa-globe"></i>External Sites<i class="fa fa-chevron-down"></i></div>
    <ul class="submenu">
      <li><a href="#">Google</a></li>
      <li><a href="#">Bing</a></li>
      <li><a href="#">Yahoo</a></li>
      <li><a href="#">GMAIL</a></li>

    </ul>
  </li>
</ul>