为什么我的 jquery 幻灯片代码不起作用?

发布于 2024-08-25 18:27:06 字数 2774 浏览 10 评论 0原文

我有三个图像,显示和样式如下:

<img id="img1" src="images/category_slideshow/yellow-viper-on-car-ramps.jpg" alt="Yellow Viper on Race Ramps Car Ramps" width="557" height="415" /> 
        <img class="onBottom" src="" width="557" height="415" />  
        <img class="onTop" src="" width="557" height="415" /> 

.onBottom{
  position:absolute;
  margin-top:-415px;
  background:url(../images/category_slideshow/loading.gif) center no-repeat;
}

.onTop{
  position:absolute;
  margin-top:-415px;
  background:url(../images/category_slideshow/loading.gif) center no-repeat;
}

这是我的 Javascript:

  $(document).ready(function() {

    var productImages = new Array();
    productImages["raceramps56"] = "images/category_slideshow/black-350z-on-car-ramps.jpg";
    productImages["raceramps67"] = "images/category_slideshow/red-corvette-on-car-ramps.jpg";
    productImages["xtenders"] = "images/category_slideshow/silver-sti-on-xtenders.jpg";
    productImages["sportramps"] = "images/category_slideshow/black-corvete-on-car-ramps.jpg";
    productImages["rollups"] = "images/category_slideshow/yellow-lotus-on-rollups-for-car-ramps.jpg";


    $(".onTop, .onBottom").css("opacity", "0");   //Give the two images 0 opacity

    $( '.leftMenuProductButton' ).hover ( 
      function () {                         
        //.leftMenuProductButton is a button in a menu.
        // There are several of these buttons, each button
        // has an ID such as "raceramps56" or "xtenders"

        var swapIMG = $(this).attr("id");  //This gets the ID, such as "raceramps56" or "xtenders"

        $(".onTop").css({opacity: '0', 'z-index': '3'})
          .attr("src", productImages[swapIMG])
          .load(function() {
            //This takes the first IMG, make it invsisbile,
            // brings it to the top, replaces it's SRC image
            // with the variable, then once it is loaded, it fades in.
             $(this).fadeTo(300, 1);
          })
          .removeClass("onTop")
          .addClass("replaceMe"); // This removes the class "onTop and replaces it with "replaceMe". I can't simply change onTop to onBottom yet, as I need to reference onBottom later.

         $(".onBottom").addClass("onTop").removeClass("onBottom").css({'z-index': '1'});  // This converts onTop to onBottom, and puts it at the bottom. 

         $(".replaceMe").removeClass("replaceMe").addClass("onBottom"); //Now that I no longer have an onBottom, I can replace replaceMe with onBottom

      },
      function () {
        //does nothing right now

      });
   });

此代码运行得很好。但是,如果我将鼠标悬停在 .leftMenuProductButton 上,然后移动到另一个 .leftMenuProductButton,然后在两者之间来回移动,则它的行为不正常。

您可以在 www.raceramps.com/v3 查看示例。

I have three images, displayed and styled as follows:

<img id="img1" src="images/category_slideshow/yellow-viper-on-car-ramps.jpg" alt="Yellow Viper on Race Ramps Car Ramps" width="557" height="415" /> 
        <img class="onBottom" src="" width="557" height="415" />  
        <img class="onTop" src="" width="557" height="415" /> 

.onBottom{
  position:absolute;
  margin-top:-415px;
  background:url(../images/category_slideshow/loading.gif) center no-repeat;
}

.onTop{
  position:absolute;
  margin-top:-415px;
  background:url(../images/category_slideshow/loading.gif) center no-repeat;
}

Here is my Javascript:

  $(document).ready(function() {

    var productImages = new Array();
    productImages["raceramps56"] = "images/category_slideshow/black-350z-on-car-ramps.jpg";
    productImages["raceramps67"] = "images/category_slideshow/red-corvette-on-car-ramps.jpg";
    productImages["xtenders"] = "images/category_slideshow/silver-sti-on-xtenders.jpg";
    productImages["sportramps"] = "images/category_slideshow/black-corvete-on-car-ramps.jpg";
    productImages["rollups"] = "images/category_slideshow/yellow-lotus-on-rollups-for-car-ramps.jpg";


    $(".onTop, .onBottom").css("opacity", "0");   //Give the two images 0 opacity

    $( '.leftMenuProductButton' ).hover ( 
      function () {                         
        //.leftMenuProductButton is a button in a menu.
        // There are several of these buttons, each button
        // has an ID such as "raceramps56" or "xtenders"

        var swapIMG = $(this).attr("id");  //This gets the ID, such as "raceramps56" or "xtenders"

        $(".onTop").css({opacity: '0', 'z-index': '3'})
          .attr("src", productImages[swapIMG])
          .load(function() {
            //This takes the first IMG, make it invsisbile,
            // brings it to the top, replaces it's SRC image
            // with the variable, then once it is loaded, it fades in.
             $(this).fadeTo(300, 1);
          })
          .removeClass("onTop")
          .addClass("replaceMe"); // This removes the class "onTop and replaces it with "replaceMe". I can't simply change onTop to onBottom yet, as I need to reference onBottom later.

         $(".onBottom").addClass("onTop").removeClass("onBottom").css({'z-index': '1'});  // This converts onTop to onBottom, and puts it at the bottom. 

         $(".replaceMe").removeClass("replaceMe").addClass("onBottom"); //Now that I no longer have an onBottom, I can replace replaceMe with onBottom

      },
      function () {
        //does nothing right now

      });
   });

This code runs pretty good. However, if I mouseover .leftMenuProductButton, then move to another .leftMenuProductButton, then move back and forth between the two, it does not behave properly.

You can view an example at www.raceramps.com/v3 .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不美如何 2024-09-01 18:27:06

嗯,好的,我要改变两件事,首先尝试改变

$(".onTop").css({opacity: '0', 'z-index': '3'}).attr("src",productImages[swapIMG]).load(function() {
             $(this).fadeTo(300, 1, function(){
    $(this).removeClass("onTop").addClass("replaceMe");
});

}); 

这种方式,直到完全淡入为止,它不会删除类。如果你像你一样链接 .removeClass ,它将在 LOAD 完成后立即触发,而不是淡入淡出。

其次,我会在开始新动画之前停止所有动画,尝试

$(".onTop").stop();

在悬停功能之上添加

hm ok there are 2 things i would change, first of all try changing

$(".onTop").css({opacity: '0', 'z-index': '3'}).attr("src",productImages[swapIMG]).load(function() {
             $(this).fadeTo(300, 1, function(){
    $(this).removeClass("onTop").addClass("replaceMe");
});

}); 

this way it wont remove the class untill completely faded in. if you chain .removeClass like you did it will immediately be triggered after LOAD is completed not the fade.

second i would stop all animations before starting a new one try adding

$(".onTop").stop();

on top of your hovering function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文