gpt4 book ai didi

javascript - Js (+Mootools) - 为什么我的脚本使用超过 60% 的处理器?

转载 作者:行者123 更新时间:2023-11-30 18:54:38 26 4
gpt4 key购买 nike

在此站点上 - LINK - 我需要使用 3 个横幅滚动条(2x 垂直 + 1x 水平)。我曾尝试在 Flash 中执行此操作,但随后每个人的网络浏览器都关闭或暂停。现在我想用 JS 来做(我使用 mootools)。所有数据均来自 MySQL。这是完整的代码(即使你不知道mootools,你也应该明白)

global $wpdb;
$table = $wpdb->prefix.'part';
$sql = "SELECT * FROM $table";
$q = $wpdb->get_results($sql);
$g = 0;
if($wpdb->num_rows > 0)
{
?>
<script type="text/javascript">
window.addEvent('load', function(){
var totall = 0;
var totalr = 0;
$$('#leftCont0 .contElement').each(function(el){
var img = new Asset.image(el.getFirst('a').getFirst('img').get('src'));
totall += img.height;
});
$$('#rightCont0 .contElement').each(function(el){
var img = new Asset.image(el.getFirst('a').getFirst('img').get('src'));
totalr += img.height;
});

$$('.leftCont').each(function(el){
var h = parseInt(el.get('id').substr(8));
el.setStyle('top', h * totall);
});
$$('.rightCont').each(function(el){
var h = parseInt(el.get('id').substr(9));
el.setStyle('top', h * totalr);
});
var total = new Array(totall, totalr);
move.periodical(30, null, total);
});
function move(num, num2)
{
var h = 0;
var da = false;
var target = null;
$$('.leftCont').each(function(el){
var act = el.getStyle('top');
var n = parseInt(act)+1;
el.setStyle('top', n+"px");
if(el.getStyle('top') < h)
{
h = parseInt(el.getStyle('top'));
alert(h);
}
if(parseInt(el.getStyle('top')) > 400)
{
da = true;
target = el;
}
});
if(da)
{
var n = h - num;
target.setStyle('top', n+'px');
}
h = 0;
da = false;
$$('.rightCont').each(function(el){
var act = el.getStyle('top');
var n = parseInt(act)+1;
el.setStyle('top', n+"px");
if(el.getStyle('top') < h)
{
h = parseInt(el.getStyle('top'));
alert(h);
}
if(parseInt(el.getStyle('top')) > 400)
{
da = true;
target = el;
}
});
if(da)
{
var n = h - num2;
target.setStyle('top', n+'px');
}
}
</script>
<?php
$g = 0;
$l = 0;
$r = 0;
$leftContent = array();
$rightContent = array();
$leftHeight = 0;
$rightHeight = 0;
foreach($q as $q)
{
if(($g % 2) == 0)
{
$leftContent[$l] = '<div class="contElement">
<a href="'.$q->aurl.'"><img src="'.$q->imgurl.'" alt="Partner" /></a>
</div>';
$lHeight = getimagesize($q->imgurl);
$leftHeight .= $lHeight[1];
$l++;
}
else
{
$rightContent[$r] = '<div class="contElement">
<a href="'.$q->aurl.'"><img src="'.$q->imgurl.'" alt="Partner" /></a>
</div>';
$rHeight = getimagesize($q->imgurl);
$rightHeight .= $rHeight[1];
$r++;
}
$g++;
}
$quantity = ceil(400 / $leftHeight) + 1;

for($i = 0; $i < $quantity; $i++)
{
$str = "";
for($j = 0; $j < sizeof($leftContent); $j++)
{
$str .= $leftContent[$j];
}
$leftContainer[$i] = '<div class="leftCont" id="leftCont'.$i.'">'.$str.'</div>';
}

$quantity = ceil(400 / $rightHeight) + 1;

for($i = 0; $i < $quantity; $i++)
{
$str = "";
for($j = 0; $j < sizeof($rightContent); $j++)
{
$str .= $rightContent[$j];
}
$rightContainer[$i] = '<div class="rightCont" id="rightCont'.$i.'">'.$str.'</div>';
}

?>
<div id="pcl">
<?php
for($i = 0; $i < sizeof($leftContainer); $i++)
{
echo $leftContainer[$i];
}
?>
</div>
<div id="pcr">
<?php
for($i = 0; $i < sizeof($rightContainer); $i++)
{
echo $rightContainer[$i];
}
?>
</div>
<?php
}

最佳答案

您的 move() 函数有两个 each() 循环,每 30 毫秒执行一次的代价很高。因为您所有的滚动元素都以相同的速度移动/彼此不相对,所以您可以只为它们的根容器设置动画。我很确定这会给您带来 10 倍的提升。

要找出究竟是什么占用了 CPU 周期,请使用浏览器提供的 javascript profiler。 Safari 有一个很好的,Firebug 也有。

关于javascript - Js (+Mootools) - 为什么我的脚本使用超过 60% 的处理器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2566090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com