当视口可见指定元素时,由于元素对视口可见,页面标题将会更改,但是如何根据标题更改标题一个除法根据分部标题更改页面标题
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
}
}
});
现在它的工作,非常感谢你!但这里还有一个问题,如果我有3个“目标”,我怎么才能使它工作,因为javaScript只是检测第一个目标,而不是其他目标:( – astroXoom
你好?你还活着吗? – astroXoom
好吧,我一定会去的。 –