Javascript 图像旋转器在旧版浏览器中运行缓慢
我用 javascript 创建了一个简单的图像旋转器,但对于“较旧的浏览器”(例如 IE 6、7 和 8)来说速度很慢。我认为图像将首先加载,然后启动旋转器..一些使其更快的技巧?
为什么我自己创建一个旋转器?我发现的所有旋转器都会剪切(裁剪)图像。我需要完整的图像......必要时需要一些左/右或上/下的空白。
JavaScript 部分:
//Loop through pictures
var tid = setInterval(mycode, 3000);
function mycode() {
if($.random(0,1) == 1){
//Fade
$('#alleplaatjes img.active').fadeOut(500,function(){
$(this).removeClass();
if($(this).next().length == 0){
$('#alleplaatjes img').first().fadeIn(500);
$('#alleplaatjes img').first().addClass('active');
} else {
$(this).next().fadeIn(500);
$(this).next().addClass('active');
}
});
} else {
//Slide
$('#alleplaatjes img.active').slideUp(500,function(){
$(this).removeClass();
if($(this).next().length == 0){
$('#alleplaatjes img').first().slideDown(500);
$('#alleplaatjes img').first().addClass('active');
} else {
$(this).next().slideDown(500);
$(this).next().addClass('active');
}
});
}
};
PHP 部分:
<?php
$dir = "/home/zwuq/domains/***/public_html/afbeelding/";
foreach(glob($dir."*") as $file){
$afbeelding = 'afbeelding/'.str_replace($dir, '', $file);
$toevoeging = FALSE;
if(!$eerste_plaatje){
$toevoeging = ' class="active"';
$eerste_plaatje = $afbeelding;
}
$plaatjes .= '<img'.$toevoeging.' src="'.$afbeelding.'" style="max-width: 99%; max-height: 99%;">';
}
?>
HTML 部分:
<div id="alleplaatjes" style="width:100%; height:100%; margin:0px; padding:0px; z-index:1; text-align: center;">
<? echo $plaatjes; ?>
</div>
I've created a simple image rotator with javascript, but it's slow with "older browsers" (for example IE 6 , 7 and 8). Images will load first I think, then start the rotator.. Some tips to make it faster?
Why I created a rotator by myself? All the rotators I've found cut (cropped) the images. I need the full image... with when necessary some white space left/right or top/bottom.
Javascript part:
//Loop through pictures
var tid = setInterval(mycode, 3000);
function mycode() {
if($.random(0,1) == 1){
//Fade
$('#alleplaatjes img.active').fadeOut(500,function(){
$(this).removeClass();
if($(this).next().length == 0){
$('#alleplaatjes img').first().fadeIn(500);
$('#alleplaatjes img').first().addClass('active');
} else {
$(this).next().fadeIn(500);
$(this).next().addClass('active');
}
});
} else {
//Slide
$('#alleplaatjes img.active').slideUp(500,function(){
$(this).removeClass();
if($(this).next().length == 0){
$('#alleplaatjes img').first().slideDown(500);
$('#alleplaatjes img').first().addClass('active');
} else {
$(this).next().slideDown(500);
$(this).next().addClass('active');
}
});
}
};
PHP part:
<?php
$dir = "/home/zwuq/domains/***/public_html/afbeelding/";
foreach(glob($dir."*") as $file){
$afbeelding = 'afbeelding/'.str_replace($dir, '', $file);
$toevoeging = FALSE;
if(!$eerste_plaatje){
$toevoeging = ' class="active"';
$eerste_plaatje = $afbeelding;
}
$plaatjes .= '<img'.$toevoeging.' src="'.$afbeelding.'" style="max-width: 99%; max-height: 99%;">';
}
?>
HTML part:
<div id="alleplaatjes" style="width:100%; height:100%; margin:0px; padding:0px; z-index:1; text-align: center;">
<? echo $plaatjes; ?>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个建议是不要使用
setInterval
。如果操作时间太长(在您的情况下,超过 3 秒),累积的延迟将导致您的动画变得更糟。要尝试我的建议,只需调用
setTimeout
而不是setInterval
,然后在mycode
末尾,再次调用 setTimeout。理想情况下,您可以跟踪函数被调用的时间并调整传递到下一个超时的时间间隔。为了在 StackOverflow 上获得最佳结果,在 http://jsfiddle.net 上发布示例将让其他人实时看到问题并可能会帮助我们帮助您。
另一个建议
缓存您的 jQuery 对象。例如,而不是:
你应该有
One suggestion is not to use
setInterval
. If the operation takes too long (in your case, longer than 3 seconds), the accumulated delay will cause your animation to get even worse.To try my suggestion just call
setTimeout
instead ofsetInterval
, then at the end of yourmycode
, you call setTimeout again. Ideally, you keep track of how late your function was called and adjust the interval passed to the next timeout.For best results here at StackOverflow, posting an example on http://jsfiddle.net will let other people see the problem live and may help us help you.
Another suggestion
Cache your jQuery objects. For example instead of:
You should have