[英]Can't get function to work on window resize
So, I have the following jQuery code: 所以,我有以下jQuery代码:
$(function() {
var lastElement = false;
$("ul li").each(function() {
if (!lastElement || lastElement.offset().top == $(this).offset().top) {
// add slash
$(lastElement).after(' /');
}
lastElement = $(this);
})
});
Basically it will add slashs after every li element, with the exception that if the li is the last in the line it will not add slash. 基本上它会在每个li元素之后添加斜杠,但是如果li是行中的最后一个,它将不会添加斜杠。 I tried to use this same function to resize, but when the window is resized, it adds several slashs without removing the other.
我尝试使用相同的函数来调整大小,但是当调整窗口大小时,它会添加几个斜杠而不删除另一个。
Here is a fiddle: http://jsfiddle.net/W2ULx/23/ 这是一个小提琴: http : //jsfiddle.net/W2ULx/23/
Any idea? 任何想法? Thanks...
谢谢...
Instead of changing the content directly 而不是直接改变内容
you can try to control the class 你可以尝试控制班级
for example: 例如:
$(lastElement).addClass('hasSlash');
Additionally to the answer of Ian Wang (and referring to your comment): 除了Ian Wang的回答(并提到你的评论):
You can, of course, use pure CSS for doing this job: 当然,您可以使用纯CSS来完成这项工作:
li:not(:last-of-type):after{
content:" / ";
}
The problem(?) is that you will have to remove the unwanted slashes manually using media queries: 问题(?)是您必须使用媒体查询手动删除不需要的斜杠:
@media screen and (max-width:500px) {
li:nth-of-type(3n):after {
content:"";
}
}
I'm not sure if it's better to use JavaScript or CSS. 我不确定使用JavaScript或CSS是否更好。 I think I'd prefer the CSS solution.
我想我更喜欢CSS解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.