2016-07-10 66 views
2

當視口可見指定元素時,由於元素對視口可見,頁面標題將會更改,但是如何根據標題更改標題一個除法根據分部標題更改頁面標題

This is the jsFiddle, Feel free copy paste it to try it tho
Live Viewing/Testing for Results

的「標題」(有些代碼是從GitHub庫「jQuery.isOnScreen」我不主張說,這是我的權利,但我試圖使用它並修改它爲我的網站,榮譽原始dev:D)

順便說一句,這裏是JavaScript代碼:

// This Gets the Article Title from a Division! 

$.fn.is_on_screen = function(){ 
    var win = $(window); 
    var viewport = { 
     top : win.scrollTop(), 
     left : win.scrollLeft() 
    }; 
    viewport.right = viewport.left + win.width(); 
    viewport.bottom = viewport.top + win.height(); 

    var bounds = this.offset(); 
    bounds.right = bounds.left + this.outerWidth(); 
    bounds.bottom = bounds.top + this.outerHeight(); 

    return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom)); 
}; 

if($('.target').length > 0) { // if target element exists in DOM 
    if($('.target').is_on_screen()) { // if target element is visible on screen after DOM loaded 
     document.title = "An Article"; // show this if visible 
    } else { 
     document.title = "Prospekt | A Gaming Community"; // show this if NOT visible 
    } 
} 
$(window).scroll(function(){ // bind window scroll event 
    if($('.target').length > 0) { // if target element exists in DOM 
     if($('.target').is_on_screen()) { // show this if it's visible to dom 
      document.title = 'It is Magic! | Stackoverflow'; // show this if visible 
     } else { 
     document.title = "Prospekt | A Gaming Community"; // show this if not visible 
     } 
    } 
}); 

回答

1

我得到的解決方案是將這段代碼

if($('.target').length > 0) { // if target element exists in DOM 
if($('.target').is_on_screen()) { // if target element is visible on screen after DOM loaded 
    document.title = "An Article"; // show this if visible 
} else { 
    document.title = "Prospekt | A Gaming Community"; // show this if NOT visible 
}} 
$(window).scroll(function(){ // bind window scroll event 
if($('.target').length > 0) { // if target element exists in DOM 
    if($('.target').is_on_screen()) { // show this if it's visible to dom 
     document.title = 'It is Magic! | Stackoverflow'; // show this if visible 
    } else { 
    document.title = "Prospekt | A Gaming Community"; // show this if not visible 
    } 
} 
}); 

有了這個代碼:

$(window).scroll(function(){ // bind window scroll event 
     if($('.target').length > 0) { // if target element exists in DOM 
if($('.target').is_on_screen()) { // if target element is visible on screen after DOM loaded 
    document.title = $('.target')[0].title; // changes the document title to the target title. 
}}}); 

編輯 爲了與更多的目標使用此代碼,而不是使這項工作。

$(window).scroll(function() { // binds window scroll event 
$.each($('.target'), function(index, value) { //for each element in the target class 
    theTarget = value //sets the variable theTarget to the value of the current index of the target class  
     if ($(theTarget).is_on_screen() && theTarget) { // if theTarget element is visible on screen after DOM loaded and(&&) theTarget exists 
      document.title = theTarget.title; // changes the document title to the theTarget's title 
     } 
}); 
}); 

編輯 爲了設置一個默認的標題中使用該代碼。編輯'defaultTitle'變量以設置默認標題,否則它會自動檢測標題。如果你的目標是兩個相距甚遠,這將導致從標題條變 - > Defualt - >第三條 代碼:

var defaultTitle = document.title; //automatically gets original title from the title element and stores it in a variable. you can also just set a title here as the default. 
$(window).scroll(function() { // binds window scroll event 
    if (!$('.target').is_on_screen()) {//if all of the targets are not on screen. 
     document.title = defaultTitle; //set the title to the default 
    } 
    $.each($('.target'), function(index, value) { //for each element in the target class 
     theTarget = value; //sets the variable theTarget to the value of the current index of the target class 
     if ($(theTarget).is_on_screen() && theTarget) { // if theTarget element is visible on screen after DOM loaded and(&&) theTarget exists 
      document.title = theTarget.title; // changes the document title to the theTarget's title 
     } 
    }); 
});//only triggers on scroll, you may want to also put it in $(document).ready() 
+0

現在它的工作,非常感謝你!但這裏還有一個問題,如果我有3個「目標」,我怎麼才能使它工作,因爲javaScript只是檢測第一個目標,而不是其他目標:( – astroXoom

+0

你好?你還活着嗎? – astroXoom

+0

好吧,我一定會去的。 –

0

你需要做的就是確定你的元素中視可見的第一件事。你已經做了這個代碼,所以我沒有打擾調整它。

接下來,您需要獲得一個標題。我認爲最好是將標題放入標記中,而不是將所有額外的JS都包括在內。理想情況下,您的網頁將位於已有標題的部分,並且您可以從此處獲取文本。

<section> 
    <h1>This is a heading for this section</h1> 
    <p>Some content goes here.</p> 
</section> 

也許在其他情況下,您不希望在標題中選擇標題。對於那些時候,我們可以從數據屬性中獲取它。

<section data-page-title="Section with data attribute title"> 
    <p> 
    This section has no heading, but its title comes from a data attribute! 
    </p> 
</section> 

我們可以用簡單的代碼處理這兩種情況:

$(window).scroll(function() { 
    var $sectionEl; 
    $('section').each(function (index, sectionEl) { 
     $sectionEl = $(sectionEl); 
     if ($sectionEl.isOnScreen()) { 
      document.title = 'Title Prefix | ' + (
      $sectionEl.data('pageTitle') || 
      $sectionEl.find('h1,h2,h3,h4,h5,h6').text().trim() 
     ); 
      return false; 
     } 
    }); 
}); 

這裏有一個小提琴:https://jsfiddle.net/mnzLkdc8/1/注意頁面的標題不能在小提琴本身被改變,但你可以用console.log()看數據。

我也推薦debouncing滾動事件,因爲滾動發生時沒有太多開銷。