[英]Check the div's height every 5 seconds and change class if true
I have this function already, which checks for change, and if true only updates this div. 我已经有了此功能,可以检查更改,如果为true,则仅更新此div。
jQuery(document).ready( function($) {
var auto_refresh = setInterval(function() {
$.ajax({
success: function(data) {
var result = $('<div />').append(data).find('div#vs').html();
$('div#vs').html(result);
}
})
}, 5000); // refreshing after every 5000 milliseconds
})
This works great, but now I want to add another function, I have made this javascript http://jsfiddle.net/jockebq/ocLh1rLd/ 这很好用,但是现在我想添加另一个功能,我已经将此javascript http://jsfiddle.net/jockebq/ocLh1rLd/
What it does is that if the height of the div #vs
exceeds 300px
it will add class .vscroll
to #vs
. 它的作用是,如果div的高度
#vs
超过300px
,将类添加.vscroll
到#vs
。 I have managed to make this work great in JSFiddle, but I cannot figure out how to merge this together with my javascript above. 我设法在JSFiddle中使这项工作出色,但是我不知道如何将其与上面的javascript合并。
I'm very much stuck, and I cannot find any information on how to do this. 我非常困惑,我找不到有关如何执行此操作的任何信息。 All help and tips are much appreciated!
非常感谢所有帮助和提示!
I am sure i am missing something here but why not just add it inside the function passed to setInterval and run it alongside the ajax call 我确定我在这里丢失了一些东西,但为什么不将其添加到传递给setInterval的函数中,并在ajax调用旁边运行它
var auto_refresh = setInterval(function() {
$.ajax({
success: function(data) {
var result = $('<div />').append(data).find('div#vs').html();
$('div#vs').html(result);
if (document.getElementById('vs').clientHeight > 300 )
$('div#vs').addClass('vscroll');
}
});
}, 5000); // refreshing after every 5000 milliseconds
})
PS: Your ajax better not be as you pasted it here! PS:最好不要将ajax粘贴到此处!
EDIT: added the code in the success callback, since you probably want to resize when the new content is appended,as said by Pierre 编辑:在成功回调中添加了代码,因为您可能想在添加新内容时调整大小,如皮埃尔所说
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.