我是JavaScript的新手,我试图创建一个简单的隐藏并显示div切换,尽管它不起作用。我不确定问题是什么 - 我最初有div display:none
,然后点击course-info-toggle
类时,它应该转到display:block
,{{1向下。
有什么想法吗?
transition

/* Toggle on course detail sections */
$(document).ready(function() {
$('.course-info-toggle').click(function() {
$('.toggle-content').toggleClass('.show');
});
});

.toggle-content.show {
display: block;
transition: left 0.3s linear;
}
.toggle-content {
display: none;
background-color: #eeeeee;
padding: 1rem;
margin-right: 1rem;
}

答案 0 :(得分:5)
删除toggleClass中的点
应该是
$('.toggle-content').toggleClass('show');
而不是.toggleClass('.show')
你想要添加动画和转换。使用关键帧进行动画制作。显示属性似乎不适用于转换。
Css transition from display none to display block, navigation with subnav
Slide in from left CSS animation
解决方案:您可以将css类更改为此。现在,你可以用不同的方式解决它。
.toggle-content.show {
visibility: visible;
position: relative;
opacity: 1;
animation: leftSlide 2s forwards;
}
.toggle-content {
opacity: 0;
visibility: hidden;
background-color: #eeeeee;
transition: opacity 5s, visibility 2ms;
}
@keyframes leftSlide {
0% {
transform: translateX(-900px);
}
100% {
transform: translateX(0);
}
}
要使其向下滑动,您可以使用translateY
。
答案 1 :(得分:1)
您需要将.show
更改为仅show
&将!important
添加到display:block
/* Toggle on course detail sections */
$(document).ready(function() {
$('.course-info-toggle').click(function() {
$('.toggle-content').toggleClass('show');
});
});

.toggle-content.show {
display: block !important;
transition: left 0.3s linear;
}
.toggle-content {
display: none;
background-color: #eeeeee;
padding: 1rem;
margin-right: 1rem;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="toggle-wrap">
<div class="course-info-toggle">
<p id="course-details-toggle-font">COURSE OBJECTIVES</p>
<img src="img/course-down-arrow.png" align="course-down-arrow">
</div>
<div class="toggle-content">
<p> simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
<p> simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
</p>
</div>
</div>
&#13;
答案 2 :(得分:0)
更改此行
$('.toggle-content').toggleClass('.show');
到行
$('.toggle-content').toggleClass('show');
答案 3 :(得分:0)
它正在按预期工作,除非我弄错了,这不是一个错字
$('.toggle-content').toggleClass('.show');
你不应该使用&#39; .show&#39;它应该是
$('.toggle-content').toggleClass('show');
您不必根据docs使用.class名称。如果你这样做,它可以正常工作。
你可以查看小提琴here。除了上面提到的那个之外,没有做任何改变。
答案 4 :(得分:0)
$(document).ready(function() {
$('.course-info-toggle').click(function() {
$('.toggle-content').toggleClass('show');
});
});
&#13;
.toggle-content.show {
display: block;
transition: left 0.3s linear;
}
.toggle-content {
display: none;
background-color: #eeeeee;
padding: 1rem;
margin-right:1rem;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="toggle-wrap">
<div class="course-info-toggle">
<p id="course-details-toggle-font">COURSE OBJECTIVES</p>
<img src="img/course-down-arrow.png" align="course-down-arrow">
</div>
<div class="toggle-content">
<p>simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
<p>simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
</p>
</div>
</div>
&#13;
答案 5 :(得分:0)
toggleClass接受一个字符串,请使用以下内容:
.toggleClass('show');
答案 6 :(得分:0)
/* Toggle on course detail sections */
$('.course-info-toggle').click(function() {
$('.toggle-content').toggleClass('show');
});
.toggle-content.show {
display: block;
transition: left 0.3s linear;
}
.toggle-content {
display: none;
background-color: #eeeeee;
padding: 1rem;
margin-right: 1rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="toggle-wrap">
<div class="course-info-toggle">
<p id="course-details-toggle-font">COURSE OBJECTIVES</p>
<img src="img/course-down-arrow.png" align="course-down-arrow">
</div>
<div class="toggle-content">
<p> simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
<p> simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled”<br>
</p>
</div>
</div>