第二个手风琴小组没有更新

时间:2017-03-28 00:56:08

标签: javascript jquery html css

在手风琴中我制作的第一个面板是一个图标选择。选择其中一个图标后,它会根据选择的图标用三个面板中的一个替换第二个手风琴面板。我认为我的问题在于javascript以及我如何设置它。我认为我的问题也可能存在于类中的div.panel类中。

HTML

<button class="accordion">
                    <h2 class="float-left">1.</h2>
                    <h2 class="text-center">Screen Type</h2></button> 
                <div class="panel text-center" id="type_panel">
                    <label class="icon-select">
                        <input type="radio" name="type" id="laptop_button" /> <img src="icons/iconmonstr-laptop-4-120.png" alt="laptop"> </label>
                    <label class="icon-select">
                        <input type="radio" name="type" id="tablet_button" /> <img src="icons/iconmonstr-tablet-1-120.png" alt="tablet"> </label>
                    <label class="icon-select">
                        <input type="radio" name="type" id="phone_button" /> <img src="icons/iconmonstr-smartphone-3-120.png" alt="phone"> </label>
                </div>
                <form action="" id="tallyForm" onsubmit="return false;">
                    <button class="accordion">
                        <h2 class="float-left">2.</h2>
                        <h2 class="text-center">Model</h2></button>
                    <div class="panel" id="default_panel">
                        <label><h3 class="text-center">Please select a Device Type above</h3></label>
                    </div>
                    <div class="panel invisible" id="laptop_panel">
                        <div id="col1">
                            <label class="control control--radio">LAPTOP
                                <input type="radio" name="radio-model" value="laptop1" />
                                <div class="control__indicator"></div>
                            </label>
                        </div>
                        <div id="col3">
                            <label class="control control--radio">LAPTOP2
                                <input type="radio" name="radio-model" value="laptop2" />
                                <div class="control__indicator"></div>
                            </label>
                        </div>
                        <div id="col2">
                            <label class="control control--radio">LAPTOP3
                                <input type="radio" name="radio-model" value="laptop3" />
                                <div class="control__indicator"></div>
                            </label>
                        </div>
                    </div>
                    <div class="panel invisible" id="tablet_panel">
                        <div id="col1">
                            <label class="control control--radio">iPad 2
                                <input type="radio" name="radio-model" value="tablet-ipad2" onclick="calculateTotal()" />
                                <div class="control__indicator"></div>
                            </label>
                            <label class="control control--radio">iPad 3
                                <input type="radio" name="radio-model" value="tablet-ipad3" onclick="calculateTotal()" />
                                <div class="control__indicator"></div>
                            </label>
                            <label class="control control--radio">iPad 4
                                <input type="radio" name="radio-model" value="tablet-ipad4" />
                                <div class="control__indicator"></div>
                            </label>
                            <label class="control control--radio">iPad Air
                                <input type="radio" name="radio-model" value="tablet-ipadAir" />
                                <div class="control__indicator"></div>
                            </label>
                            <label class="control control--radio">iPad Mini
                                <input type="radio" name="radio-model" value="tablet-ipadMini" />
                                <div class="control__indicator"></div>
                            </label>
                            <label class="control control--radio">iPad Mini 2
                                <input type="radio" name="radio-model" value="tablet-ipadMini2" />
                                <div class="control__indicator"></div>
                            </label>
                        </div>
                        <div id="col3">
                            <label class="control control--radio">Nexus 7
                                <input type="radio" name="radio-model" value="tablet-nexus7" />
                                <div class="control__indicator"></div>
                            </label>
                        </div>
                        <div id="col2">
                            <label class="control control--radio">Amazon Fire
                                <input type="radio" name="radio-model" value="tablet-amazonFire" />
                                <div class="control__indicator"></div>
                            </label>
                            <label class="control control--radio">Amazon Kindle
                                <input type="radio" name="radio-model" value="tablet-amazonFire" />
                                <div class="control__indicator"></div>
                            </label>
                        </div>
                    </div>
                    <div class="panel invisible" id="phone_panel">
                        <div id="col1">
                            <label class="control control--radio">iPhone 3 &amp; 4
                                <input type="radio" name="radio-model" value="phone-iphone3" />
                                <div class="control__indicator"></div>
                            </label>
                            <label class="control control--radio">iPhone 5, 5c, 5s
                                <input type="radio" name="radio-model" value="phone-iphone5cs" />
                                <div class="control__indicator"></div>
                            </label>
                            <label class="control control--radio">iPhone 6
                                <input type="radio" name="radio-model" value="phone-iphone6" />
                                <div class="control__indicator"></div>
                            </label>
                            <label class="control control--radio">iPhone 6 Plus
                                <input type="radio" name="radio-model" value="phone-iphone6+" />
                                <div class="control__indicator"></div>
                            </label>
                        </div>
                        <div id="col3">
                            <label class="control control--radio">Microsoft Lumia 430
                                <input type="radio" name="radio-model" value="phone-lumia430" />
                                <div class="control__indicator"></div>
                            </label>
                        </div>
                        <div id="col2">
                            <label class="control control--radio">Galaxy S3
                                <input type="radio" name="radio-model" value="phone-galaxys3" />
                                <div class="control__indicator"></div>
                            </label>
                            <label class="control control--radio">Galaxy S4
                                <input type="radio" name="radio-model" value="phone-galaxys4" />
                                <div class="control__indicator"></div>
                            </label>
                        </div>
                    </div>

CSS

.icon-select {
    margin-right: 20px;
    margin-left: 20px;
}

#col1 {
    float: left;
    width: 33%;
    height: 100%;
}

#col2 {
    float: center;
    height: 100%;
    width: 33%;
    overflow: hidden;
    display: table-cell;
}

#col3 {
    float: right;
    height: 100%;
    width: 34%;
    margin-left: 20px;
    overflow: hidden;
    display: table-cell;
}

button.accordion {
    background-color: #ffffff;
    color: #444;
    cursor: pointer;
    padding: 2px;
    width: 50%;
    text-align: left;
    outline: none;
    transition: 0.4s;
    border-left: 1px solid #6fdd7a;
    border-right: 1px solid #6fdd7a;
    border-top: 1px solid #6fdd7a;
    border-radius: 4px;
    border-bottom: none;
}

button.accordion.active,
button.accordion:hover {
    background-color: #6fdd7a;
    color: #ffffff;
}

div.panel {
    padding: 0px 18px;
    background-color: #ffffff;
    max-height: 0;
    overflow: hidden;
    width: 46%;
    border-right: 1px dotted #6fdd7a;
    border-left: 1px dotted #6fdd7a;
    transition: max-height 0.2s ease-out;
}

#optional_panel {
    border-bottom: 1px solid #6fdd7a;
}

label > input {
    visibility: hidden;
    position: absolute;
}

label > input + img {
    cursor: pointer;
    border: 2px solid transparent;
    border-radius: 2px;
    -webkit-transition: all 0.25s linear;
}

label > input:checked + img {
    background-color: #6fdd7a;
}

.invisible {
    display: none;
}

.showing {
    padding: 0px 18px;
    background-color: #ffffff;
    max-height: 600px;
    overflow: hidden;
    width: 46%;
    border-right: 1px dotted #6fdd7a;
    border-left: 1px dotted #6fdd7a;
    transition: max-height 0.2s ease-out;
}
// JUST STYLES THE RADIO BUTTONS AND 
.control {
    font-size: 18px;
    position: relative;
    display: block;
    margin-bottom: 15px;
    padding-left: 30px;
    cursor: pointer;
}

.control input {
    position: absolute;
    z-index: -1;
    opacity: 0;
}

.control__indicator {
    position: absolute;
    top: 2px;
    left: 0;
    width: 20px;
    height: 20px;
    background: #e6e6e6;
}

.control--radio .control__indicator {
    border-radius: 50%;
}

.control:hover input ~ .control__indicator,
.control input:focus ~ .control__indicator {
    background: #444;
}

.control input:checked ~ .control__indicator {
    background: #6fdd7a;
}

.control:hover input:not([disabled]):checked ~ .control__indicator,
.control input:checked:focus ~ .control__indicator {
    background: #6fdd7a;
}

.control__indicator:after {
    position: absolute;
    display: none;
    content: '';
}

.control input:checked ~ .control__indicator:after {
    display: block;
}

.control--checkbox .control__indicator:after {
    top: 4px;
    left: 8px;
    width: 3px;
    height: 8px;
    transform: rotate(45deg);
    border: solid #fff;
    border-width: 0 2px 2px 0;
}

.control--checkbox input:disabled ~ .control__indicator:after {
    border-color: #7b7b7b;
}

.control--radio .control__indicator:after {
    top: 7px;
    left: 7px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #fff;
}

.control--radio input:disabled ~ .control__indicator:after {
    background: #7b7b7b;
}

JS

var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
    acc[i].onclick = function () {
        this.classList.toggle("active");
        var panel = this.nextElementSibling;
        if (panel.style.maxHeight) {
            panel.style.maxHeight = null;
        }
        else {
            panel.style.maxHeight = panel.scrollHeight + "px";
        }
    };
}
$("#laptop_button").click(function () {
    $('#default_panel').addClass('invisible');
    $('#laptop_panel').removeClass('invisible');
    $('#laptop_panel').addClass('showing');
    $('#tablet_panel').addClass('invisible');
    $('#phone_panel').addClass('invisible');
});
$("#tablet_button").click(function () {
    $('#default_panel').addClass('invisible');
    $("#tablet_panel").removeClass('invisible');
    $('#tablet_panel').addClass('showing');
    $('#laptop_panel').addClass('invisible');
    $('#phone_panel').addClass('invisible');
});
$("#phone_button").click(function () {
    $('#default_panel').addClass('invisible');
    $("#phone_panel").removeClass('invisible');
    $('#phone_panel').addClass('showing');
    $('#laptop_panel').addClass('invisible');
    $('#tablet_panel').addClass('invisible');
});

的jsfiddle

https://jsfiddle.net/jutmfuzc/

1 个答案:

答案 0 :(得分:1)

由于max-height.showing

,您需要在div.panel上设置max-height: 0

var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
  acc[i].onclick = function() {
    this.classList.toggle("active");
    var panel = this.nextElementSibling;
    if (panel.style.maxHeight) {
      panel.style.maxHeight = null;
    } else {
      panel.style.maxHeight = panel.scrollHeight + "px";
    }
  };
}
$("#laptop_button").click(function() {
  $('#default_panel').addClass('invisible');
  $('#laptop_panel').removeClass('invisible');
  $('#laptop_panel').addClass('showing');
  $('#tablet_panel').addClass('invisible');
  $('#phone_panel').addClass('invisible');
});
$("#tablet_button").click(function() {
  $('#default_panel').addClass('invisible');
  $("#tablet_panel").removeClass('invisible');
  $('#tablet_panel').addClass('showing');
  $('#laptop_panel').addClass('invisible');
  $('#phone_panel').addClass('invisible');
});
$("#phone_button").click(function() {
  $('#default_panel').addClass('invisible');
  $("#phone_panel").removeClass('invisible');
  $('#phone_panel').addClass('showing');
  $('#laptop_panel').addClass('invisible');
  $('#tablet_panel').addClass('invisible');
});
.icon-select {
  margin-right: 20px;
  margin-left: 20px;
}

#col1 {
  float: left;
  width: 33%;
  height: 100%;
}

#col2 {
  float: center;
  height: 100%;
  width: 33%;
  overflow: hidden;
  display: table-cell;
}

#col3 {
  float: right;
  height: 100%;
  width: 34%;
  margin-left: 20px;
  overflow: hidden;
  display: table-cell;
}

button.accordion {
  background-color: #ffffff;
  color: #444;
  cursor: pointer;
  padding: 2px;
  width: 50%;
  text-align: left;
  outline: none;
  transition: 0.4s;
  border-left: 1px solid #6fdd7a;
  border-right: 1px solid #6fdd7a;
  border-top: 1px solid #6fdd7a;
  border-radius: 4px;
  border-bottom: none;
}

button.accordion.active,
button.accordion:hover {
  background-color: #6fdd7a;
  color: #ffffff;
}

div.panel {
  padding: 0px 18px;
  background-color: #ffffff;
  max-height: 0;
  overflow: hidden;
  width: 46%;
  border-right: 1px dotted #6fdd7a;
  border-left: 1px dotted #6fdd7a;
  transition: max-height 0.2s ease-out;
}

#optional_panel {
  border-bottom: 1px solid #6fdd7a;
}

label > input {
  visibility: hidden;
  position: absolute;
}

label > input + img {
  cursor: pointer;
  border: 2px solid transparent;
  border-radius: 2px;
  -webkit-transition: all 0.25s linear;
}

label > input:checked + img {
  background-color: #6fdd7a;
}

.invisible {
  display: none;
}

div.showing {
  padding: 0px 18px;
  background-color: #ffffff;
  max-height: 600px;
  overflow: hidden;
  width: 46%;
  max-height: 100%;
  border-right: 1px dotted #6fdd7a;
  border-left: 1px dotted #6fdd7a;
  transition: max-height 0.2s ease-out;
}

// JUST STYLES THE RADIO BUTTONS AND 
.control {
  font-size: 18px;
  position: relative;
  display: block;
  margin-bottom: 15px;
  padding-left: 30px;
  cursor: pointer;
}

.control input {
  position: absolute;
  z-index: -1;
  opacity: 0;
}

.control__indicator {
  position: absolute;
  top: 2px;
  left: 0;
  width: 20px;
  height: 20px;
  background: #e6e6e6;
}

.control--radio .control__indicator {
  border-radius: 50%;
}

.control:hover input ~ .control__indicator,
.control input:focus ~ .control__indicator {
  background: #444;
}

.control input:checked ~ .control__indicator {
  background: #6fdd7a;
}

.control:hover input:not([disabled]):checked ~ .control__indicator,
.control input:checked:focus ~ .control__indicator {
  background: #6fdd7a;
}

.control__indicator:after {
  position: absolute;
  display: none;
  content: '';
}

.control input:checked ~ .control__indicator:after {
  display: block;
}

.control--checkbox .control__indicator:after {
  top: 4px;
  left: 8px;
  width: 3px;
  height: 8px;
  transform: rotate(45deg);
  border: solid #fff;
  border-width: 0 2px 2px 0;
}

.control--checkbox input:disabled ~ .control__indicator:after {
  border-color: #7b7b7b;
}

.control--radio .control__indicator:after {
  top: 7px;
  left: 7px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #fff;
}

.control--radio input:disabled ~ .control__indicator:after {
  background: #7b7b7b;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="accordion">
  <h2 class="float-left">1.</h2>
  <h2 class="text-center">Screen Type</h2></button>
<div class="panel text-center" id="type_panel">
  <label class="icon-select">
    <input type="radio" name="type" id="laptop_button" /> <img src="icons/iconmonstr-laptop-4-120.png" alt="laptop"> </label>
  <label class="icon-select">
    <input type="radio" name="type" id="tablet_button" /> <img src="icons/iconmonstr-tablet-1-120.png" alt="tablet"> </label>
  <label class="icon-select">
    <input type="radio" name="type" id="phone_button" /> <img src="icons/iconmonstr-smartphone-3-120.png" alt="phone"> </label>
</div>
<form action="" id="tallyForm" onsubmit="return false;">
  <button class="accordion">
    <h2 class="float-left">2.</h2>
    <h2 class="text-center">Model</h2></button>
  <div class="panel" id="default_panel">
    <label>
      <h3 class="text-center">Please select a Device Type above</h3></label>
  </div>
  <div class="panel invisible" id="laptop_panel">
    <div id="col1">
      <label class="control control--radio">LAPTOP
        <input type="radio" name="radio-model" value="laptop1" />
        <div class="control__indicator"></div>
      </label>
    </div>
    <div id="col3">
      <label class="control control--radio">LAPTOP2
        <input type="radio" name="radio-model" value="laptop2" />
        <div class="control__indicator"></div>
      </label>
    </div>
    <div id="col2">
      <label class="control control--radio">LAPTOP3
        <input type="radio" name="radio-model" value="laptop3" />
        <div class="control__indicator"></div>
      </label>
    </div>
  </div>
  <div class="panel invisible" id="tablet_panel">
    <div id="col1">
      <label class="control control--radio">iPad 2
        <input type="radio" name="radio-model" value="tablet-ipad2" onclick="calculateTotal()" />
        <div class="control__indicator"></div>
      </label>
      <label class="control control--radio">iPad 3
        <input type="radio" name="radio-model" value="tablet-ipad3" onclick="calculateTotal()" />
        <div class="control__indicator"></div>
      </label>
      <label class="control control--radio">iPad 4
        <input type="radio" name="radio-model" value="tablet-ipad4" />
        <div class="control__indicator"></div>
      </label>
      <label class="control control--radio">iPad Air
        <input type="radio" name="radio-model" value="tablet-ipadAir" />
        <div class="control__indicator"></div>
      </label>
      <label class="control control--radio">iPad Mini
        <input type="radio" name="radio-model" value="tablet-ipadMini" />
        <div class="control__indicator"></div>
      </label>
      <label class="control control--radio">iPad Mini 2
        <input type="radio" name="radio-model" value="tablet-ipadMini2" />
        <div class="control__indicator"></div>
      </label>
    </div>
    <div id="col3">
      <label class="control control--radio">Nexus 7
        <input type="radio" name="radio-model" value="tablet-nexus7" />
        <div class="control__indicator"></div>
      </label>
    </div>
    <div id="col2">
      <label class="control control--radio">Amazon Fire
        <input type="radio" name="radio-model" value="tablet-amazonFire" />
        <div class="control__indicator"></div>
      </label>
      <label class="control control--radio">Amazon Kindle
        <input type="radio" name="radio-model" value="tablet-amazonFire" />
        <div class="control__indicator"></div>
      </label>
    </div>
  </div>
  <div class="panel invisible" id="phone_panel">
    <div id="col1">
      <label class="control control--radio">iPhone 3 &amp; 4
        <input type="radio" name="radio-model" value="phone-iphone3" />
        <div class="control__indicator"></div>
      </label>
      <label class="control control--radio">iPhone 5, 5c, 5s
        <input type="radio" name="radio-model" value="phone-iphone5cs" />
        <div class="control__indicator"></div>
      </label>
      <label class="control control--radio">iPhone 6
        <input type="radio" name="radio-model" value="phone-iphone6" />
        <div class="control__indicator"></div>
      </label>
      <label class="control control--radio">iPhone 6 Plus
        <input type="radio" name="radio-model" value="phone-iphone6+" />
        <div class="control__indicator"></div>
      </label>
    </div>
    <div id="col3">
      <label class="control control--radio">Microsoft Lumia 430
        <input type="radio" name="radio-model" value="phone-lumia430" />
        <div class="control__indicator"></div>
      </label>
    </div>
    <div id="col2">
      <label class="control control--radio">Galaxy S3
        <input type="radio" name="radio-model" value="phone-galaxys3" />
        <div class="control__indicator"></div>
      </label>
      <label class="control control--radio">Galaxy S4
        <input type="radio" name="radio-model" value="phone-galaxys4" />
        <div class="control__indicator"></div>
      </label>
    </div>
  </div>