jquery如何在扩展器函数中预加载然后交换图像?

发布于 2024-08-09 00:55:18 字数 1902 浏览 10 评论 0原文

我正在使用 Expander.js 一个用于扩展文本的 jquery 插件。

现在事情是这样的,

每当触发展开和折叠时我都应该交换图像。

现在通常这不是问题。

还有一条信息是,有一个项目列表,其中包含可扩展的描述和图像,因此代码内有 id。

编辑:现在将显示完整的代码。

代码如下:

   $(document).ready(function() {

  // override some default options
  $('div.expandable div').expander({
    slicePoint:       200,  // default is 100
    expandText:         'Expand', // default is 'read more...'

    expandEffect:     'fadeIn',
    collapseTimer:    0, // re-collapses after 5 seconds; default is 0, so no re-collapsing
    userCollapseText: 'Collapse' , // default is '[collapse expanded text]'

    afterExpand: function($thisElement) {

      var vendorParaID = $thisElement.attr('id');

      var underscore = vendorParaID.indexOf('_');

      var vendorID = vendorParaID.substring(0, underscore);

      $("#vendor_img_"+vendorID).attr({height : "308",
                      src : "img/m/"+vendorID+".jpg"
                      });


    },
    onCollapse: function($thisElement, byUser) {
      //alert($thisElement.attr('id'));
      var vendorParaID = $thisElement.attr('id');
      var underscore = vendorParaID.indexOf('_');

      var vendorID = vendorParaID.substring(0, underscore);


      $("#vendor_img_"+vendorID).attr({height : "80",
                      src : "img/m/"+vendorID+"-crop.jpg"
                      });


    }
  });

});

但是,图像的变化之间存在滞后。

我喜欢预加载图像,但随后我不确定如何正确交换它。

我查找了这两个链接,但我仍然不确定如何使其工作。

http://jquery-howto.blogspot.com/ 2009/02/preload-images-with-jquery.html http://jquery- howto.blogspot.com/2009/04/jquery-image-swap-or-how-to-replace.html

请告知。

i am using expander.js a jquery plugin for expanding text.

Now here is the thing,

whenever the expand and collapse are triggered i am supposed to swap an image.

now usually that is not a problem.

One more piece of information is that there is a list of items which comes with expandable description and image hence the id inside the code.

EDIT: Will now display the full code.

The code is below:

   $(document).ready(function() {

  // override some default options
  $('div.expandable div').expander({
    slicePoint:       200,  // default is 100
    expandText:         'Expand', // default is 'read more...'

    expandEffect:     'fadeIn',
    collapseTimer:    0, // re-collapses after 5 seconds; default is 0, so no re-collapsing
    userCollapseText: 'Collapse' , // default is '[collapse expanded text]'

    afterExpand: function($thisElement) {

      var vendorParaID = $thisElement.attr('id');

      var underscore = vendorParaID.indexOf('_');

      var vendorID = vendorParaID.substring(0, underscore);

      $("#vendor_img_"+vendorID).attr({height : "308",
                      src : "img/m/"+vendorID+".jpg"
                      });


    },
    onCollapse: function($thisElement, byUser) {
      //alert($thisElement.attr('id'));
      var vendorParaID = $thisElement.attr('id');
      var underscore = vendorParaID.indexOf('_');

      var vendorID = vendorParaID.substring(0, underscore);


      $("#vendor_img_"+vendorID).attr({height : "80",
                      src : "img/m/"+vendorID+"-crop.jpg"
                      });


    }
  });

});

However there is a lag between the change in the image.

I like to preload the image but then i am not sure how to swap it correctly.

I looked up these 2 links but i am still not sure how to make it work.

http://jquery-howto.blogspot.com/2009/02/preload-images-with-jquery.html
http://jquery-howto.blogspot.com/2009/04/jquery-image-swap-or-how-to-replace.html

Please advise.

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

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

发布评论

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

评论(2

帅哥哥的热头脑 2024-08-16 00:55:18

我终于想通了。

首先在文档准备功能之前有这个

$preloadImgVendor = new Array();
  $preloadImgVendorCrop = new Array();

  {/literal}{foreach from=$venders item=vender name=vender}
  {literal}var id = {/literal}{$vender.id_manufacturer}{literal};
  $preloadImgVendor[id] = $("<img />").attr({height : "308",
                        width : "180",
                src : "img/m/"+id+".jpg",
                id : "vendor_img_"+id
                });
   $preloadImgVendorCrop[id] =  $("<img />").attr({height : "80",
                         width : "180",
                src : "img/m/"+id+"-crop.jpg",
                id : "vendor_img_"+id
                      });

  {/literal}{/foreach}{literal}

然后在扩展器内的相关行使用

$("#vendor_img_"+vendorID).replaceWith($preloadImgVendor[vendorID]);

$("#vendor_img_"+vendorID).replaceWith($preloadImgVendorCrop[vendorID]);

I finally figured it out.

first have this before the document ready function

$preloadImgVendor = new Array();
  $preloadImgVendorCrop = new Array();

  {/literal}{foreach from=$venders item=vender name=vender}
  {literal}var id = {/literal}{$vender.id_manufacturer}{literal};
  $preloadImgVendor[id] = $("<img />").attr({height : "308",
                        width : "180",
                src : "img/m/"+id+".jpg",
                id : "vendor_img_"+id
                });
   $preloadImgVendorCrop[id] =  $("<img />").attr({height : "80",
                         width : "180",
                src : "img/m/"+id+"-crop.jpg",
                id : "vendor_img_"+id
                      });

  {/literal}{/foreach}{literal}

Then inside the expander at the relevant lines use

$("#vendor_img_"+vendorID).replaceWith($preloadImgVendor[vendorID]);

or

$("#vendor_img_"+vendorID).replaceWith($preloadImgVendorCrop[vendorID]);
爱情眠于流年 2024-08-16 00:55:18
 ...
 <script>

    $(function() {

        $preloadImgVendor = new Array();
        $preloadImgVendorCrop = new Array();

        //automatically call function and variable objects with images with vendors Ids in "listIdsVendors"
        (function(aVendors){

            $.each(aVendors, function(i,val)
            {
                //check
                alert("vendor id load : " + i);

                $preloadImgVendor[i] = $("<img />").attr("src", "img/m/"+i+".jpg");
                $preloadImgVendorCrop[i] = $("<img />").attr("src", "img/m/"+i+"-crop.jpg");
            });

            //check
            alert("$preloadImgVendor length: " + $preloadImgVendor.length + "\n$preloadImgVendorCrop length: " + $preloadImgVendorCrop.length);


        })($("#listIdsVendors").text().split("-"));

      // override some default options
      $('div.expandable div').expander({
        slicePoint:       200,  // default is 100
        expandText:         'Expand', // default is 'read more...'

        expandEffect:     'fadeIn',
        collapseTimer:    0, // re-collapses after 5 seconds; default is 0, so no re-collapsing
        userCollapseText: 'Collapse' , // default is '[collapse expanded text]'

        afterExpand: function($thisElement) {
          var vendorParaID = $thisElement.attr('id');
          var underscore = vendorParaID.indexOf('_');
          var vendorID = vendorParaID.substring(0, underscore);

          //check
          alert($thisElement.attr('id') + " ; " + $($preloadImgVendor[vendorID]).attr("src"));

          $("#vendor_img_"+vendorID).attr({height : "308", src : ($preloadImgVendor[vendorID]).attr("src") });
        },
        onCollapse: function($thisElement, byUser) {
          var vendorParaID = $thisElement.attr('id');
          var underscore = vendorParaID.indexOf('_');   
          var vendorID = vendorParaID.substring(0, underscore);

          //check
          alert($thisElement.attr('id') + " ; " + $($preloadImgVendorCrop[vendorID]).attr("src"));

          $("#vendor_img_"+vendorID).attr({height : "80", src : $($preloadImgVendorCrop[vendorID]).attr("src") });
        }
      });

    });
</script>
</head>
<body>
    <div id="listIdsVendors" style="display:none">1-2-3-4-5-6</div>
    ...
</body>
 ...
 <script>

    $(function() {

        $preloadImgVendor = new Array();
        $preloadImgVendorCrop = new Array();

        //automatically call function and variable objects with images with vendors Ids in "listIdsVendors"
        (function(aVendors){

            $.each(aVendors, function(i,val)
            {
                //check
                alert("vendor id load : " + i);

                $preloadImgVendor[i] = $("<img />").attr("src", "img/m/"+i+".jpg");
                $preloadImgVendorCrop[i] = $("<img />").attr("src", "img/m/"+i+"-crop.jpg");
            });

            //check
            alert("$preloadImgVendor length: " + $preloadImgVendor.length + "\n$preloadImgVendorCrop length: " + $preloadImgVendorCrop.length);


        })($("#listIdsVendors").text().split("-"));

      // override some default options
      $('div.expandable div').expander({
        slicePoint:       200,  // default is 100
        expandText:         'Expand', // default is 'read more...'

        expandEffect:     'fadeIn',
        collapseTimer:    0, // re-collapses after 5 seconds; default is 0, so no re-collapsing
        userCollapseText: 'Collapse' , // default is '[collapse expanded text]'

        afterExpand: function($thisElement) {
          var vendorParaID = $thisElement.attr('id');
          var underscore = vendorParaID.indexOf('_');
          var vendorID = vendorParaID.substring(0, underscore);

          //check
          alert($thisElement.attr('id') + " ; " + $($preloadImgVendor[vendorID]).attr("src"));

          $("#vendor_img_"+vendorID).attr({height : "308", src : ($preloadImgVendor[vendorID]).attr("src") });
        },
        onCollapse: function($thisElement, byUser) {
          var vendorParaID = $thisElement.attr('id');
          var underscore = vendorParaID.indexOf('_');   
          var vendorID = vendorParaID.substring(0, underscore);

          //check
          alert($thisElement.attr('id') + " ; " + $($preloadImgVendorCrop[vendorID]).attr("src"));

          $("#vendor_img_"+vendorID).attr({height : "80", src : $($preloadImgVendorCrop[vendorID]).attr("src") });
        }
      });

    });
</script>
</head>
<body>
    <div id="listIdsVendors" style="display:none">1-2-3-4-5-6</div>
    ...
</body>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文