简体   繁体   English

javascript-页面加载时位置设置不正确

[英]javascript - position not being set properly on page load

I am creating a coverflow plugin but I have a slight problem when it first loads. 我正在创建一个Coverflow插件,但第一次加载时有一个小问题。

The size/styles of the images is set based on their position in the coverflow. 根据图像在Coverflow中的位置设置图像的尺寸/样式。 When the page first loads the images all resize properly but they do not reposition themselves. 页面首次加载时,所有图像均会正确调整大小,但不会自行调整位置。 If I them use the left and right navigation they work correctly. 如果我使用左右导航,它们将正常工作。

I am not sure what is causing this. 我不确定是什么原因造成的。 I thought it might be something to do with the variable that sets the starting position of the coverflow... 我认为这可能与设置Coverflow起始位置的变量有关...

Here's my code: 这是我的代码:

    <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>

And here's a link to a jsfiddle: 这是jsfiddle的链接:

http://jsfiddle.net/r8NqP/4/ http://jsfiddle.net/r8NqP/4/

Calling testme() at the end of the ready() function moves them into place. 在ready()函数末尾调用testme()会将它们移动到位。 It does ease them in though, which looks a bit odd, could get rid of the ease in testme() by adding a doease parameter. 它确实使它们看起来很奇怪,但是可以通过添加doease参数来消除testme()中的便捷性。

Check you fist each : each检查你的拳头:

'z-index' : 100-(Math.abs(coverflowPos-i)) }).width(200-(Math.abs(coverflowPos-i)*50)).height(128-(Math.abs(coverflowPos-i)*50));

I think U mean: 我想你的意思是:

'z-index' : 100-(Math.abs(coverflowPos-i)),
'width' : 200-(Math.abs(coverflowPos-i)*50),
'height': 128-(Math.abs(coverflowPos-i)*50)

Linke In your testme() function ?! 在您的testme()函数中链接?

After that, you can also add a "Hack", by executing testme(true); 之后,您还可以通过执行testme(true);添加“ Hack” testme(true); at the end of script. 在脚本末尾。 And add, in your testme() function , a test parameter to set the duration at 0 or simply disable animate and replace by CSS(). 并在您的testme()函数中添加一个测试参数,以将持续时间设置为0或仅禁用动画并替换为CSS()。

But, it just a Hack. 但是,这只是一个hack。

200-(Math.abs(coverflowPos-i)*50) may be less than 0 -- eg, 200-(Math.abs(coverflowPos-i)*50)可能小于0-例如,

200-(5-0)* 50= 200 - 250 = -50

And the negative width ends up not being applied, leaving the width at its original 200px value. 负宽度最终没有应用,使宽度保持其原始200px值。 The opacity gets set properly, so all you get is a huge blank space where the image is. 正确设置了不透明度,因此您得到的只是图像所在的巨大空白空间。

var width = 200-(Math.abs(coverflowPos-i)*50);
if ( width < 0 ) width = 0;

covers the init nicely. 很好地覆盖了初始化。

I haven't bothered to check why it's okay once it's animated -- my guess is, that the images were already small, so it's not as noticeable. 我没有费心去检查为什么一旦动画就可以了-我的猜测是,图像已经很小了,所以不那么引人注目。

The problem came from "Each index", that not correctly used to compute the Width and Height of the first image. 问题来自“每个索引”,该索引未正确用于计算第一个图像的宽度和高度。

Try this : 尝试这个 :

 $('#coverflow img').each( function(i) {
    i++;
    $(this).css({...

And remove the Blank.gif... 并删除Blank.gif ...

Here, you find my fork fiddle : http://jsfiddle.net/akarun/FQWQa/ 在这里,您可以找到我的叉子小提琴: http : //jsfiddle.net/akarun/FQWQa/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM