2014-07-14 65 views
0

在我的網站上,幾秒鐘後會出現一個花哨的框彈出窗口,詢問用戶他是否想註冊通訊。這工作正常。通過普通文字鏈接打開fancybox

但是,我也想在網頁上放一個鏈接,以便打開時事通訊欄。我試過Click <a href="#newsletterpopup">here</a> to open the popup。沒有成功。

http://jsfiddle.net/PQqGC/1/

非常感謝您的幫助

JS:

function openFancybox() { 
    setTimeout(function() { 
     $.fancybox('#newsletterpopup', { 
      closeClick: false, // this prevents fancybox to close unless close unless ".non-merci" is clicked 
      showCloseButton: true, 
      helpers: { 
       overlay: { 
        css: { 
         'background': 'rgba(58, 42, 45, 0.3)' 
        } 
       } 
      }, 
      afterShow: function() { 
       // enables a way to close fancybox 
       $(".non-merci").on("click", function() { 
        $.fancybox.close() 
       }); 
      } 
     }); 
    }, 7000); 
}; 
$(document).ready(function() { 
    var visited = $.cookie('visited'); 
    if (visited == 'yes') { 
     return false; 
    } else { 
     openFancybox(); 
    } 
    $.cookie('visited', 'yes', { 
     expires: 0.04 
    }); 
}); 

非關聯腳本,可能導致與點擊功能衝突:

//============== 
//! Smooth scrolling/click potentially conflicting with other scripts 
//============== 

$(document).ready(function(e) { 
$('a[href*=#]:not([href=#])').click(function (ev) { // Added 'ev' parameter 
    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) { 
     var target = $(this.hash); 
     target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); 
     if (target.length) { 
      ev.preventDefault(); // We're animating this, so don't let the browser try to navigate to this URL 
      $('html,body').animate({ 
       scrollTop: target.offset().top - 100 
      }, 'normal'); 
     } 
    } 
}); 


window.onscroll = scrollFunction; 
function scrollFunction() { 
    var doc = document.documentElement, body = document.body; 
    var top = (doc && doc.scrollTop || body && body.scrollTop || 0); 
    if (top > 200) { 
     $('.back-to-top').fadeIn(); 
    } 
    else { 
     $('.back-to-top').fadeOut(); 
    } 
} 
}); 
+0

你的小提琴顯示錯誤'undefined不是一個函數'。嘗試先糾正它。 – j809

回答

2

你需要稍微修改腳本:

首先,你需要選擇添加到您的鏈接

<a class="fancybox" href="#newsletterpopup">here</a> 

...所以你可以在選擇綁定到正規的fancybox初始化像

$(".fancybox").fancybox({ 
    // options 
}); 

其次,您的openFancybox()函數不會以編程方式打開fancybox,但會在已經初始化的風扇上觸發click事件cybox選擇像

function openFancybox() { 
    setTimeout(function() { 
     $(".fancybox").trigger("click"); 
    }, 7000); 
}; 

在您涵蓋你的需求的方式:在幾秒鐘後火災的fancybox並具有鏈接手動打開通訊

編輯:在的jsfiddle

修正錯誤看到更新JSFIDDLE

+0

謝謝JFK。我試圖實現它,但由於某種原因,它不能在實時環境中工作,請參閱http://goo.gl/aiUGDv我想在scripts.js中可能有一些衝突的腳本,其中包含您的代碼? – Greg

+0

我的錯誤,它的工作原理!謝謝 – Greg

+0

糟糕,JFK,我說話很快,它不工作這個頁面:http://goo.gl/cZgzqq(向下滾動到按鈕,鏈接只是在表單按鈕下)。任何想法是什麼造成衝突?謝謝 – Greg

1

我將fancybox聲明放在一個單獨的函數中,以便它可以是c從你的計時器和點擊事件中取消。

小提琴:http://jsfiddle.net/PQqGC/3/

第3行和46打電話給你的fancybox代碼。

**我不得不將您的cookie代碼註釋掉,因爲這個庫沒有加載。