简体   繁体   English

调用 jQuery 超大插件 onclick

[英]calling jQuery supersized plugin onclick

I am trying to call the supersized plugin ( http://buildinte.net.com/2009/02/supersized-full-screen-backgroundslideshow-jquery-plugin/ ) onclick, so far when I click on my different menus the images are changed, but it looks like html markup build by supersized don't get rebuilt, right now I have this:我正在尝试调用超大插件 ( http://buildinte.net.com/2009/02/supersized-full-screen-backgroundslideshow-jquery-plugin/ ) onclick,到目前为止,当我点击不同的菜单时,图像是改变了,但看起来 html 由 supersized 构建的标记没有被重建,现在我有这个:

html which calls a function brown() onclick: html 调用 function brown() onclick:

<ul id="rooms_menu" style="display:none;">
<li><a href="javascript:;" onclick="brown()">menu title</a></li>
<p class="rooms_desc">description text</p>

the html where I echo an ajax response: html,我回显 ajax 响应:

<div id="script">

<script>$(function(){
        $.supersized({
                      slides    :   [
                     {image     : 'img/rooms-default.jpg'}
                     //{image     : 'img/rooms-default.jpg'}
                                    ]
                     })
                     })</script>

</div>

brown() is an ajax function: brown() 是一个 ajax function:

 function brown(){
      $.ajax({
      url: 'ajax.php?action=brown',
      success: function(data){
        $('#script').html(data);
             }
           })
    
         }

then the ajax.php file loads its content to #script div:然后 ajax.php 文件将其内容加载到#script div:

switch($_GET["action"]){
      case "brown":
      echo "<script>$(function(){
         $.supersized({
                   slides :     [
               {image  : 'img/rooms-brown-01.jpg'},
               {image  : 'img/rooms-brown-02.jpg'}
                            ]
                            })
         
                            })</script>";
      break;
      case "rose":  etc.....

so the images are updated when I click for the first time on a menu but if I click another menu title the images are updated too but the slideshow starts messing up, it looks like the html markup is not rebuilt, when I use only one image per menu title(only one image in supersized array) there is no problem.所以当我第一次点击菜单时图像会更新,但如果我点击另一个菜单标题图像也会更新但幻灯片开始混乱,看起来 html 标记没有重建,当我只使用一个图像时每个菜单标题(超大数组中只有一个图像)没有问题。 R. R。

before adding the new script with在添加新脚本之前

$('#script').html(data);

try to call this:试着这样称呼:

if($.supersized.vars.slideshow_interval){
  clearInterval($.supersized.vars.slideshow_interval);
}

the method then should look like:该方法应如下所示:

function brown(){
  $.ajax({
    url: 'ajax.php?action=brown',
    success: function(data){
      if($.supersized.vars.slideshow_interval){
        clearInterval($.supersized.vars.slideshow_interval);
      }
      $('#script').html(data);
    }
  });

}

Instead of returning the whole script from PHP, you can simply return the image tags (like不是从 PHP 返回整个脚本,你可以简单地返回图像标签(比如

<img src="images/image1.jpg" /><img src="images/image1.jpg" />

...). ...)。 Then in the ajax success callback in place of - $('#script').html(data);然后在 ajax 成功回调中替换 - $('#script').html(data); - you can do - 你可以做

$('#script').empty();
$('#script').html(data);
$('#script').supersized();

I believe it's the $.supersize.resizenow method which sets everything up, have you tried calling that?我相信这是设置一切的$.supersize.resizenow方法,你试过调用它吗?

Failing that, do you have a link that is web accessable where you are trying this so I could have a look?如果做不到这一点,您是否有一个可以访问的链接 web 可以在您尝试此操作的地方访问,以便我看一下?

Have you tried unbinding the event before attaching a new one?您是否尝试过在附加新事件之前取消绑定事件? like this:像这样:

switch($_GET["action"]){
      case "brown":
      echo "<script>$(function(){
         $(window).unbind("resize");//add this. you could also add         
                                     //$('#supersized').html('') to remove markup created by the plugin
         $.supersized({
                   slides :     [
               {image  : 'img/rooms-brown-01.jpg'},
               {image  : 'img/rooms-brown-02.jpg'}
                            ]
                            })

                            })</script>";
      break;

You might have to do this because the plugin binds an event to the window and even if you remove the previous html created by the plugin the old event is still binded and it fires twice.您可能必须这样做,因为插件将一个事件绑定到 window,即使您删除了插件创建的先前 html,旧事件仍然绑定并且会触发两次。

Tell me if you notice any difference如果您发现任何差异,请告诉我

EDIT - i have looked at the code of the plugin.编辑 - 我看过插件的代码。 The problem lies, i think, in the command on line 16:我认为问题出在第 16 行的命令中:

  setInterval("theslideshow()", options.slideinterval);

a function is called at a given interval, and if it's not cleared, when you call the plugin again, a new interval is set and so you have problems with the timing. a function 在给定的时间间隔被调用,如果它没有被清除,当你再次调用插件时,一个新的时间间隔被设置,所以你有时间问题。 You could correct the plugin like this.您可以像这样更正插件。 Instead of代替

    if (options.slideshow == 1){
        setInterval("theslideshow()", options.slideinterval);
    }

put

        firstRunofThisPlugin = true;
    if (options.slideshow == 1){
                if (firstRunofThisPlugin){
                     firstRunofThisPlugin = false;
                }else{
                     clearInterval(linkToTheLastInterval);
                }
        linkToTheLastInterval = setInterval("theslideshow()", options.slideinterval);
    }

What you are doing is save a reference of the setInterval() call and then clear that reference the next time you call the plugin.您正在做的是保存 setInterval() 调用的引用,然后在您下次调用插件时清除该引用。

This uses two global variables which is bad, but if it solves the problem i can help you rewrite this without using globals.这使用了两个不好的全局变量,但如果它解决了问题,我可以帮助您在不使用全局变量的情况下重写它。

PS why don't you update the version of the plugin? PS你为什么不更新插件的版本?

Why don't you just load all of the sets and show/hide them as needed?为什么不加载所有集合并根据需要显示/隐藏它们?

<script>
  $(function(){
    function selectSet() {
      $(".set").hide();
      $(".set."+$(this).attr("id")).show();
    };
    $(".set.brown").each(selectSet);
  });
</script>

<a href="javascript:;" id="brown" onclick="selectSet()">Choose me!</a>
<a href="javascript:;" id="rose" onclick="selectSet()">No choose me!</a>

<div class="set brown">
  <script>
    $(function() {
      $.supersized({
        slides: [
          {image: 'img/rooms-1.jpg'},
          {image: 'img/rooms-2.jpg'}
        ]
      });
    });
  </script>
</div>

<div class="set rose">
  <script>
    $(function() {
      $.supersized({
        slides: [
          {image: 'img/rooms-3.jpg'},
          {image: 'img/rooms-4.jpg'}
        ]
      });
    });
  </script>
</div>

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

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