2015-07-03 92 views
0

我有以下代碼如何在關閉fancybox時停止頁面重定向?

<script type="text/javascript"> 
     jQuery(document).ready(function() { 
      beginCartnotification(); 
     }); 
     setTimeout(function() { 
      window.location.href = "http://www.someurl.com"; 
     }, 5000); 
    </script> 


<script type="text/javascript"> 
     jQuery.fancybox({ 
      'width': '300', 
      'height': '90', 
      'padding': '40px', 
      'autoDimensions': false, 
      'border': '1px solid #005195', 
      'autoScale': false, 
      'transitionIn': 'fade', 
      'transitionOut': 'fade', 
      'showCloseButton': true, 
      'type': 'inline', 
      'href': '#cartnotification-popup', 
      'onClosed': function(){ //perform some task on closing the fancybox.} 
     }); 
    } 
</script> 

現在我想要做的是,如果對看中框中關閉按鈕用戶點擊,頁面不應該重定向。

如果用戶在5秒後沒有點擊關閉按鈕,用戶應該被重定向到另一個頁面。

我該怎麼實現裏面關閉函數?

http://fancybox.net/api

回答

2

只需添加一個setTimout的fancyboxonComplete財產,一旦顯示的內容將被調用。如果在關閉fancybox之前達到超時,請重新定向。

onClose屬性函數中清除超時值,以確保window.loaction更改不會被觸發。

同時將fancybox設置添加到jQuery的.ready()方法中,以確保在文檔準備就緒後立即進行初始化。

<script type="text/javascript"> 
jQuery(document).ready(function() { 

    beginCartnotification(); 

    // ADD A VAR TO HOLD YOUR TIMER. 
    var timer; 

    jQuery.fancybox({ 
    'width': '300', 
    'height': '90', 
    'padding': '40px', 
    'autoDimensions': false, 
    'border': '1px solid #005195', 
    'autoScale': false, 
    'transitionIn': 'fade', 
    'transitionOut': 'fade', 
    'showCloseButton': true, 
    'type': 'inline', 
    'href': '#cartnotification-popup', 
    'onComplete': function() { 
     timer = setTimeout(function() { 
     // REDIRECTING TAKES PLACE AFTER 5 SEC. 
     window.location.href = "http://www.someurl.com"; 
     }, 5000); 
    }, 
    'onClosed': function() { 
     // CLEAR THE TIMEOUT !!! 
     clearTimeout(timer); 
    } 
    }); 

}); 
</script> 
+0

加一,不過,我想保持'。就緒()'方法中的fancybox初始化腳本。另外請注意,您的代碼末尾可能會出現雜亂的右括號'}' – JFK

+0

@JFK絕對正確。 Thx編輯了這篇文章。 – DavidDomain

相關問題