首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用twitter引导程序使网站专注于当前浏览的div?

如何使用twitter引导程序使网站专注于当前浏览的div?
EN

Stack Overflow用户
提问于 2013-09-26 18:34:38
回答 1查看 758关注 0票数 0

我正试图实现类似于这个例子的目标。

此单页网站在调整大小时保持当前视图。例如,如果我在调整浏览器窗口大小的同时阅读:#p_partnerships部分,它仍将显示#p_partnerships的内容。

我修改了一些单页引导模板

它工作起来像一个魅力,但当我调整它的大小,它的行为就像任何其他可滚动的页面。我的意思是回到在调整大小之前可见的部分,我必须单击某个链接,然后再单击该部分的链接。我不关心鼠标滚动效果,从我的普罗旺斯,我所需要的是保持顶部的当前查看部分,就在导航栏。

以下是我所得到的一些片段:

除了jQuery ScrollTo,我的模板使用jQuery One Page插件

代码语言:javascript
复制
;(function($, window, document, undefined){

// our plugin constructor
var OnePageNav = function(elem, options){
this.elem = elem;
this.$elem = $(elem);
this.options = options;
this.metadata = this.$elem.data('plugin-options');
this.$nav = this.$elem.find('a');
this.$win = $(window);
this.sections = {};
this.didScroll = false;
this.$doc = $(document);
this.docHeight = this.$doc.height();
};

// the plugin prototype
OnePageNav.prototype = {
defaults: {
currentClass: 'current',
changeHash: false,
easing: 'swing',
filter: '',
scrollSpeed: 750,
scrollOffset: 0,
scrollThreshold: 0.5,
begin: false,
end: false,
scrollChange: false
},

init: function() {
var self = this;

// Introduce defaults that can be extended either
// globally or using an object literal.
self.config = $.extend({}, self.defaults, self.options, self.metadata);

//Filter any links out of the nav
if(self.config.filter !== '') {
self.$nav = self.$nav.filter(self.config.filter);
}

//Handle clicks on the nav
self.$nav.on('click.onePageNav', $.proxy(self.handleClick, self));

//Get the section positions
self.getPositions();

//Handle scroll changes
self.bindInterval();

//Update the positions on resize too
self.$win.on('resize.onePageNav', $.proxy(self.getPositions, self));

eturn this;
},
adjustNav: function(self, $parent) {
self.$elem.find('.' + self.config.currentClass).removeClass(self.config.currentClass);
$parent.addClass(self.config.currentClass);
},

bindInterval: function() {
var self = this;
var docHeight;

self.$win.on('scroll.onePageNav', function() {
self.didScroll = true;
});

self.t = setInterval(function() {
docHeight = self.$doc.height();

//If it was scrolled
if(self.didScroll) {
self.didScroll = false;
self.scrollChange();
}

//If the document height changes
if(docHeight !== self.docHeight) {
self.docHeight = docHeight;
self.getPositions();
}
}, 250);
},

getHash: function($link) {
return $link.attr('href').split('#')[1];
},

getPositions: function() {
var self = this;
var linkHref;
var topPos;
var $target;

self.$nav.each(function() {
linkHref = self.getHash($(this));
$target = $('#' + linkHref);

if($target.length) {
topPos = $target.offset().top;
self.sections[linkHref] = Math.round(topPos) - self.config.scrollOffset;
}
});
},

getSection: function(windowPos) {
var returnValue = null;
var windowHeight = Math.round(this.$win.height() * this.config.scrollThreshold);

for(var section in this.sections) {
if((this.sections[section] - windowHeight) < windowPos) {
returnValue = section;
}
}

return returnValue;
},

handleClick: function(e) {
var self = this;
var $link = $(e.currentTarget);
var $parent = $link.parent();
var newLoc = '#' + self.getHash($link);

if(!$parent.hasClass(self.config.currentClass)) {
//Start callback
if(self.config.begin) {
self.config.begin();
}

//Change the highlighted nav item
self.adjustNav(self, $parent);

//Removing the auto-adjust on scroll
self.unbindInterval();

//Scroll to the correct position
$.scrollTo(newLoc, self.config.scrollSpeed, {
axis: 'y',
easing: self.config.easing,
offset: {
top: -self.config.scrollOffset
},
onAfter: function() {
//Do we need to change the hash?
if(self.config.changeHash) {
window.location.hash = newLoc;
}

//Add the auto-adjust on scroll back in
self.bindInterval();

//End callback
if(self.config.end) {
self.config.end();
}
}
});
}
e.preventDefault();
},

scrollChange: function() {
var windowTop = this.$win.scrollTop();
var position = this.getSection(windowTop);
var $parent;

//If the position is set
if(position !== null) {
$parent = this.$elem.find('a[href$="#' + position + '"]').parent();

//If it's not already the current section
if(!$parent.hasClass(this.config.currentClass)) {
//Change the highlighted nav item
this.adjustNav(this, $parent);

//If there is a scrollChange callback
if(this.config.scrollChange) {
this.config.scrollChange($parent);
}
}
}
},

unbindInterval: function() {
clearInterval(this.t);
this.$win.unbind('scroll.onePageNav');
}
};

OnePageNav.defaults = OnePageNav.prototype.defaults;


$.fn.onePageNav = function(options) {

return this.each(function() {
new OnePageNav(this, options).init();
});
};

})( jQuery, window , document );

在html文档中,我有以下几点:

代码语言:javascript
复制
<script>
$('#top-nav').onePageNav({
currentClass: 'active',
changeHash: true,
scrollSpeed: 1200
});

</script>

我试过亚历山大建议的一段代码,但它对我不起作用。

第一部分:

代码语言:javascript
复制
<script>
function goToByScroll(id){
$('html, body').stop().animate({scrollTop: $(id).offset().top}, 2000);
}
</script>

与这样的链接一起工作吗

代码语言:javascript
复制
<a href="#" onClick="goToByScroll('#section-2')">

但这和我已经拥有的一样。

我做过这样的实验

代码语言:javascript
复制
$(window).on("resize", function () {
goToByScroll('#section-2')

每次我调整浏览器的大小时,它都会滚动到#section-two的顶部。

需要一些解决方案才能滚动到当前查看的部分,而不是总是指向相同的#section-2

EN

回答 1

Stack Overflow用户

发布于 2013-09-26 19:12:19

要做到这一点,您需要两样东西:一个是滚动到div的函数,另一个是一个侦听器,用于知道某人何时滚动。

首先,一个可以滚动到您的div的jQuery函数。您可以使用类似于此的内容:

代码语言:javascript
复制
function goToByScroll(id){
  $('html, body').stop().animate({scrollTop: $(id).offset().top}, 2000);
}

有关更多信息,请参见Jquery -滚动到div或搜索堆栈溢出。然后你需要知道什么时候你在滚动。可以为此使用jQuery,然后调用函数goToByScroll()

代码语言:javascript
复制
var lastScrollTop = 0;
$(window).scroll(function(event){
  var st = $(this).scrollTop();
  if (st > lastScrollTop){
    // downscroll code
    goToByScroll(next_page_id);
  } else {
    // upscroll code
    goToByScroll(last_page_id);
  }
  lastScrollTop = st;
});

请参阅如何确定jQuery滚动事件的方向?,这应该足以给您一个好的开始。如果你向上或向下滚动,你只需要跟踪你在哪一页上,以及下一页是什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19036017

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档