jquery缓和不起作用

时间:2009-11-07 15:34:21

标签: jquery jquery-plugins

下面我使用点击项目的id告诉我的功能<div>设置动画。

然而它似乎永远不会有动画......

基本上,您点击<img>并使用该图片的ID previewItem来说明动画的<div>'#p + previewItem'

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="jquery.easing.1.2.js" ></script> 

<script type="text/javascript">
<!--

    $(document).ready(function()
    {
        var previewItem;

        $(this).click(function(event) {
            //get the ID of the clicked area
            previewItem = event.target.id;

            //alert("#p" + previewItem);
            $("#p" + previewItem).animate({left:"50%"},{duration: 3000,easing: easeOutBounce});

            //alert(previewItem);
        });
    });

    // wrap as a jQuery plugin and pass jQuery in to our anoymous function
    (function ($) {
        $.fn.cross = function (options) {
            return this.each(function (i) { 
                // cache the copy of jQuery(this) - the start image
                var $$ = $(this);

                // get the target from the backgroundImage + regexp
                var target = $$.css('backgroundImage').replace(/^url|[\(\)'"]/g, '');

                // nice long chain: wrap img element in span
                $$.wrap('<span style="position: relative;"></span>')
                    // change selector to parent - i.e. newly created span
                    .parent()
                    // prepend a new image inside the span
                    .prepend('<img>')
                    // change the selector to the newly created image
                    .find(':first-child')
                    // set the image to the target
                    .attr('src', target);

                // the CSS styling of the start image needs to be handled
                // differently for different browsers
                if ($.browser.msie || $.browser.mozilla) {
                    $$.css({
                        'position' : 'absolute', 
                        'left' : 0,
                        'background' : '',
                        'top' : this.offsetTop
                    });
                } else if ($.browser.opera && $.browser.version < 9.5) {
                    // Browser sniffing is bad - however opera < 9.5 has a render bug 
                    // so this is required to get around it we can't apply the 'top' : 0 
                    // separately because Mozilla strips the style set originally somehow...                    
                    $$.css({
                        'position' : 'absolute', 
                        'left' : 0,
                        'background' : '',
                        'top' : "0"
                    });
                } else { // Safari
                    $$.css({
                        'position' : 'absolute', 
                        'left' : 0,
                        'background' : ''
                    });
                }

                // similar effect as single image technique, except using .animate 
                // which will handle the fading up from the right opacity for us
                $$.hover(function () {
                    $$.stop().animate({
                        opacity: 0
                    }, 250);
                }, function () {
                    $$.stop().animate({
                        opacity: 1
                    }, 2000);
                });
            });
        };

    })(jQuery);

    // note that this uses the .bind('load') on the window object, rather than $(document).ready() 
    // because .ready() fires before the images have loaded, but we need to fire *after* because
    // our code relies on the dimensions of the images already in place.
    $(window).bind('load', function () {
        $('img.fade').cross();
    });

//-->
</script>

3 个答案:

答案 0 :(得分:3)

虽然其他答案也是正确的,但如果你使用的是jquery缓动插件(在http://gsgd.co.uk/sandbox/jquery/easing/找到),你应该注意到缓动选项是一个字符串。

所以你的电话会是:

$("#p" + previewItem).animate({left:"50%"},{duration: 3000,easing: 'easeOutBounce'});

答案 1 :(得分:0)

在这部分代码中:

 $(document).ready(function()
{
    var previewItem;

    $(this).click(function(event) 
    {
        //get the ID of the clicked area
            previewItem = event.target.id;

            //alert("#p" + previewItem);
            $("#p" + previewItem).animate({left:"50%"},{duration: 3000,easing: easeOutBounce});

            //alert(previewItem);
    });
});

$(this)调用不是div。如果您想知道是否点击了页面上的任何div,您可以尝试:

  $(document).ready(function()
{
    var previewItem;

    $("div").click(function(event) 
    {
        //get the ID of the clicked area
            previewItem = event.target.id;

            //alert("#p" + previewItem);
            $("#p" + previewItem).animate({left:"50%"},{duration: 3000,easing: easeOutBounce});

            //alert(previewItem);
    });
});

然后任何div都会开火。

答案 2 :(得分:0)

我不是jQuery大师,但你不应该至少拥有$('div').click(function(event)吗?

我还在我的脚本中加了一个alert('hellooo'),如果它曾经触发过它。

嗯....你说你把.bind放在Window上,因为你需要加载所有图像。我确实使用了$(document).ready

准备意味着所有东西都已加载,现在你可以用它来做什么?