我正在创建一个coverflow插件,但第一次加载时遇到了一些小问题。javascript - 在页面加载时未正确设置位置
图像的大小/风格是根据它们在封面流中的位置设置的。当页面第一次加载图片时,所有的图片都调整好了,但是它们并没有重新定位。如果我他们使用左侧和右侧导航,他们正常工作。
我不确定是什么原因造成的。我想这可能是是与那台的CoverFlow的起始位置的变量...
这里是我的代码:
<script type="text/javascript" src="/scripts/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var coverflowPos = Math.round($('#coverflow img').length/2)
$('#coverflow img').each(function(i) {
$(this).css({'opacity' : 1-(Math.abs(coverflowPos-i)*0.4), 'z-index' : 100-(Math.abs(coverflowPos-i)) }).width(200-(Math.abs(coverflowPos-i)*50)).height(128-(Math.abs(coverflowPos-i)*50));
});
// If I run the testme() function here, it animates to the right place but I want it to start in this position rather than animate to it
$('#moveLeft').click(function() {
if(coverflowPos > 1) {
coverflowPos = coverflowPos-1
}
testme();
});
$('#moveRight').click(function() {
if(coverflowPos < $("#coverflow img").length -1) {
coverflowPos = coverflowPos+1
}
testme();
});
function testme() {
$('#coverflow img').each(function(i) {
$(this).animate({
opacity: 1-(Math.abs(coverflowPos-i)*0.4),
width: 200-(Math.abs(coverflowPos-i)*50),
height: 128-(Math.abs(coverflowPos-i)*50)
}, {
duration: 500,
easing: 'easeInOutSine'
}).css({ 'z-index' : 100-(Math.abs(coverflowPos-i)) });
});
};
});
</script>
而这里的一个的jsfiddle链接:
这会工作,虽然它是一种笨重的方式去创造预期的结果。除非任何人能想到更好的东西,否则我会去做。 – Tom 2011-03-10 11:38:14
问题是宽度和高度的初始值(可能是负值,因此永远不会设置)。调用testme()是可行的,因为它在动画期间第二次设置宽度,这次是非负面的。 – Athena 2011-03-11 07:45:29