2016-07-10 169 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滚动事件,因为滚动发生时没有太多开销。