0

我正在尝试添加一个链接到另一个的淡出功能。点击这里目前我有一个闪烁的标志。当用户点击徽标时,闪烁停止,稍有延迟,然后慢慢淡出。有没有人能够纠正我在下面粘贴的代码?

    <script>
         $(document).ready(function(){
         $("#center-gif").click(function(){
            $('#center-gif').hide();
            $('#center-img').show();
            });
         $('#center-img').click(function(){
            $('#center-img').hide();
            $('#center-img-gif').show();
        });
         $('flash-link').click(function(){
            $('center-img').fadeOut(5000);
         });
    });
        </script>
4

2 回答 2

4

如果你想用class/访问元素id;您必须始终在开始时定义这些.#例如 css。

一些例子:

$('img').fadeOut();//selects all img elements
$('.img').fadeOut();//selects all elements with class="img"

$('myClass').fadeOut(); //false
$('.myClass').fadeOut(); //true

$('myId').fadeOut(); //false
$('#myId').fadeOut(); //true

这是用更少的代码为您的问题工作的 jQuery:

$(document).ready(function(){
     $("img").click(function(){
        var takeId = $(this).attr('id');//takes clicked element's id

        $('img').hide();//hides all content

        $('#'+takeId).show();
        //matches clicked element's id with element and shows that
     });

     $('#flash-link').click(function(){//define '#' id declaration here
        $('#center-img').fadeOut(5000,//new function after fadeOut complete
          function() {
             window.open('url','http://iamnatesmithen.com/jukebox/dancers.php');
             return false;
          });
        );
     });
});
于 2012-07-18T11:13:40.817 回答
0

所以我认为您的问题是该图像不会淡出,对吗?

这可以解决它:

首先将您的.click()-functions 更改为:

$().click( function(event) {
    // cour code
    event.preventDefault();
}

而不是像这样更改最后一个:

$('#flash-link').click( function(event) {
    $('#center-img').fadeOut( 5000, function() {
        window.location.href = 'jukebox/dancers.php';
    });
    event.preventDefault();
});

我没有对此进行测试,但它应该可以工作。它的作用是:淡出图像并在准备好时调用函数。然后,此功能将重定向到您的下一页。

event.preventDefault();告诉浏览器不要委托click- 事件。如果你不把它放在那里,浏览器会打开锚点而不等待任何 JavaScript 执行。

笔记

当您想选择一个带有 ID 的元素时,请使用此选择器:$('#[id]')因为此选择器$('html')仅适用于 HTML 元素。

于 2012-07-18T11:18:12.063 回答