有人可以帮我解决这个问题吗? 我只是学习CSS和Js所以我真的不知道该怎么做。
这个东西在显示内容之前上下2次,然后再次上下移动,然后隐藏内容。
CSS:
.pureyad {
max-width: 600px;
line-height: 1.1em;
font-family: 'Roboto Slab';
overflow: hidden;
background: #46988f;
font-size: 14px;
height: 120px;
padding-left: 20px
}
.purgex {
display: block;
background: transparent;
border: none;
border-top: 1px solid #222;
width: 585px;
max-width: 100%;
font-family: 'baloo bhai';
font-size: 15px
}
JS:
function ssss() {
var text = $('.pureyad'),
btn = $('.purgex'),
h = text[0].scrollHeight;
if(h > 120) {
btn.addClass('less');
}
btn.click(function(e)
{
e.stopPropagation();
if (btn.hasClass('less')) {
btn.removeClass('less');
btn.addClass('more');
btn.text('Show less');
text.animate({'height': h});
} else {
btn.addClass('less');
btn.removeClass('more');
btn.text('Show more');
text.animate({'height': '120px'});
}
});
}
HTML:
<div class="pureyad">
<p>Lorem ipsum dolor sit amet..</p>
</div>
<center><button class="purgex" onclick="ssss()">Show more</button></center>
Gif上下:http://i.giphy.com/l0HlJmVHLDGnSZWuY.gif
我从这里的回复帖子中得到这个脚本: https://stackoverflow.com/a/38680734/5118751 该脚本在Jsfiddle上工作正常,当我直接在html上加载脚本时。
答案 0 :(得分:0)
在点击功能上为btn指定按钮类时,无需为按钮添加onclick功能。
var text = $('.pureyad'),
btn = $('.purgex'),
h = text[0].scrollHeight;
if(h > 120) {
btn.addClass('less');
btn.css('display', 'block');
}
btn.click(function(e)
{
e.stopPropagation();
if (btn.hasClass('less')) {
btn.removeClass('less');
btn.addClass('more');
btn.text('Show less');
text.animate({'height': h});
} else {
btn.addClass('less');
btn.removeClass('more');
btn.text('Show more');
text.animate({'height': '120px'});
}
});
.pureyad {
max-width: 600px;
line-height: 1.1em;
font-family: 'Roboto Slab';
overflow: hidden;
background: #46988f;
font-size: 14px;
height: 120px;
padding-left: 20px
}
.purgex {
display: block;
background: transparent;
border: none;
border-top: 1px solid #222;
width: 585px;
max-width: 100%;
font-family: 'baloo bhai';
font-size: 15px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="pureyad">
<p>Lorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit ametLorem ipsum dolor sit amet..</p>
</div>
<center><button class="purgex">Show more</button></center>
答案 1 :(得分:0)
$(document).ready(function(){
var text = $('.pureyad'),
btn = $('.purgex'),
h = text[0].scrollHeight;
if(h > 120) {
btn.addClass('less');
}
btn.click(function(e)
{
e.stopPropagation();
if (btn.hasClass('less')) {
btn.removeClass('less');
btn.addClass('more');
btn.text('Show less');
text.animate({'height': h});
} else {
btn.addClass('less');
btn.removeClass('more');
btn.text('Show more');
text.animate({'height': '120px'});
}
});
});
.pureyad {
max-width: 600px;
line-height: 1.1em;
font-family: 'Roboto Slab';
overflow: hidden;
background: #46988f;
font-size: 14px;
height: 120px;
padding-left: 20px
}
.purgex {
display: block;
background: transparent;
border: none;
border-top: 1px solid #222;
width: 585px;
max-width: 100%;
font-family: 'baloo bhai';
font-size: 15px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="pureyad">
<p>Lorem ipsum dolor sit amet..dfdffsdfdf
dsfdsfdsfsdfdsf
fsdfsdfdsfsdfdfdsf
fdfsdfdsfd
Lorem ipsum dolor sit amet..dfdffsdfdf
dsfdsfdsfsdfdsf
fsdfsdfdsfsdfdfdsf
fdfsdfdsfd
Lorem ipsum dolor sit amet..dfdffsdfdf
dsfdsfdsfsdfdsf
fsdfsdfdsfsdfdfdsf
fdfsdfdsfd
Lorem ipsum dolor sit amet..dfdffsdfdf
dsfdsfdsfsdfdsf
fsdfsdfdsfsdfdfdsf
fdfsdfdsfd
Lorem ipsum dolor sit amet..dfdffsdfdf
dsfdsfdsfsdfdsf
fsdfsdfdsfsdfdfdsf
df
ds
fsdfsdfdsfds
fsdf
f
fdfsdfdsfd
Lorem ipsum dolor sit amet..dfdffsdfdf
dsfdsfdsfsdfdsf
fsdfsdfdsfsdfdfdsf
df
ds
fsdfsdfdsfds
fsdf
f
fdfsdfdsfd
Lorem ipsum dolor sit amet..dfdffsdfdf
dsfdsfdsfsdfdsf
fsdfsdfdsfsdfdfdsf
df
ds
fsdfsdfdsfds fsdf
f
fdfsdfdsfdLorem ipsum dolor sit amet..dfdffsdfdf
dsfdsfdsfsdfdsf
fsdfsdfdsfsdfdfdsf
df
ds
fsdfsdfdsfds
fsdf
f
fdfsdfdsfd
ds
fsdfsdfdsfds
fsdf
f
fdfsdfdsfd
</p>
</div>
<center><button class="purgex">Show more</button></center>
希望这会有所帮助。如果问题得到解决,请将此标记为已解决。
答案 2 :(得分:0)
我最大的错误是我在加载时不放数组(jQuery)。 :( ... 现在一切正常。
非常感谢你。