当我调整屏幕大小时,jQuery一直在准备另一个按钮
jQuery keeps prepending another button when I resize screen
当屏幕小于480px时,我试图隐藏段落,并在前面加上一个按钮"阅读更多"以打开隐藏的段落。
我已经尝试了以下代码,然而,在屏幕调整大小后,按钮现在适用于所有其他屏幕大小。关于我哪里出了问题,有什么建议吗?
$(document).ready(function(){
/* Hide content and append a button to show more content
======================================================================== */
$(window).resize(function() {
if ($(window).width() <= 480) {
$('#first_text_paragraphs').append('<button>Read More</button>');
/* toggle Read more button
============================================= */
$('button').on("click", function() {
$('.about_text_paragraphs').slideToggle();
});
}
});
});
一般提示:
-
与其附加元素,不如让它始终存在,并根据需要使用
display
CSS属性来切换它。 -
如果在宽度下降到480像素以下时执行某些操作,则需要在宽度超过480像素时执行相反的操作,否则您所做的操作将继续存在。
-
如果要在满足某些条件时向页面添加元素,则应使用标志(或类似机制)来确保不会多次添加该元素。例如,如果用户将大小调整为470像素,然后再次调整为460像素。您不希望将按钮追加两次。
-
媒体查询是你的朋友。他们可以用更少的代码做你想做的事。不过,我假设您对这里的程序化方法感兴趣。
具体来说,我会大致这样编码:
$(document).ready(function(){
$('#readMore').on("click", function() {
$('.about_text_paragraphs').slideToggle();
});
if ($(window).width() <= 480) {
$("#readMore").show();
}
/* Hide content and append a button to show more content
======================================================================== */
$(window).resize(function() {
if ($(window).width() <= 480) {
$("#readMore").show();
}
else {
$("#readMore").hide();
}
});
});
这是一把小提琴:http://jsfiddle.net/FFuvG/
或者,您可以接受bjb568的优秀建议,只需使用CSS媒体查询即可完成同样的任务。
@media screen and (max-width: 480px) {
#mytext {
max-height: 200px;
overflow: hidden;
}
#mybutton {
display: block;
}
}
HTML:
<div id="mytext">lorem ipsum</div>
<button id="mybutton" hidden>Read More</button>
Fiddle
这应该比jQuery快得多(就像所有CSS一样)。
试试这个:
演示:http://jsfiddle.net/YEwUZ/514/
这是一个按钮显示的地方:http://jsfiddle.net/YEwUZ/515/
看看这个演示是否就是你试图通过移动输出区域的宽度来实现的。
HTML
<div class="button">
This is a Button
</div>
<div class="mydiv">
Lorem Ipsum is 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 it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
CSS
.button {
background:#fefefe;
width:100px;
text-align:center;
margin:5px;
padding:5px 15px;
border:1px solid #eee;
}
@media only screen and (max-width:480px) {
.mydiv {display:none;}
}
}
查询
$(document).ready(function(){
$('.button').click(function() {
if ( $( ".mydiv" ).is( ":hidden" ) ) {
$('.mydiv').fadeIn();
} else {
$('.mydiv').fadeOut();
}
});
});
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 多个单选按钮组相互干扰
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- Javascript按钮下拉列表
- jquery试图按名称获取按钮位置
- 漂亮照片图片库中的Facebook赞按钮
- 扩展移相器按钮类不工作
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 单击按钮以等待单击按钮
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 如何从querySelectorAll中获取按钮类型
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 按下按钮时保存cookie
- 是否可以禁用jquery中的单个单选按钮
- ASP.NET通过单击JavaScript按钮触发c#事件
- 当我点击jsf中的primefaces命令按钮时,如何获得点击事件
- 我的按钮一直消失后点击
- 当我调整屏幕大小时,jQuery一直在准备另一个按钮