jquery - 如何使用 twitter bootstrap 制作网站,将重点放在当前查看的 div 上?

标签 jquery css twitter-bootstrap twitter-bootstrap-3

我正在努力实现类似 this example 的目标.

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

我修改了一些single page bootstrap template .

它就像一个魅力,但当我调整它的大小时,它的行为与任何其他可滚动页面一样。我的意思是回到调整大小之前可见的部分,我必须单击某个链接,然后再次单击该部分的链接。我不关心 myprovence 的鼠标滚动效果,我只需要将当前查看的部分的顶部保持在导航栏的正下方。

以下是我得到的一些零碎信息:

除了jQuery ScrollTo,我的模板还使用jQuery One Page Nav Plugin

;(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 文档中我有这个位:

<script>
$('#top-nav').onePageNav({
currentClass: 'active',
changeHash: true,
scrollSpeed: 1200
});

</script>

我已经尝试过 Alexander 建议的一段代码,但它对我不起作用。

第一部分:

<script>
function goToByScroll(id){
$('html, body').stop().animate({scrollTop: $(id).offset().top}, 2000);
}
</script>

确实可以使用这样的链接

<a href="#" onClick="goToByScroll('#section-2')">

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

我试过这样的东西

$(window).on("resize", function () {
goToByScroll('#section-2')

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

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

最佳答案

您需要两件事才能完成这项工作:一个滚动到 div 的函数,以及一个知道何时有人滚动的监听器。

首先,一个可以滚动到您的 div 的 jQuery 函数。您可以使用类似这样的东西:

function goToByScroll(id){
  $('html, body').stop().animate({scrollTop: $(id).offset().top}, 2000);
}

参见 Jquery - scroll to div ,或搜索 Stackoverflow 以获取更多信息。 然后你需要知道你什么时候滚动。您可以为此使用 jQuery,然后调用函数 goToByScroll()

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;
});

参见 How can I determine the direction of a jQuery scroll event?这应该足以让你有一个好的开始。如果向上或向下滚动,您只需跟踪当前所在的页面以及下一页。

关于jquery - 如何使用 twitter bootstrap 制作网站,将重点放在当前查看的 div 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19036017/

相关文章:

javascript - 修复 IE 的 div 宽度 @media 修复

javascript - 如何解析 JSON 并将其存储在外部变量中?

html - 背景附件 : fixed; not working with background-position

html - 我如何定位第一个没有特定类(class)的 child ?

css - 多背景css3动画

javascript - 下拉菜单打开时自动选择输入字段的文本

javascript - 是什么关闭了模态框?

javascript - 基于基于列表的菜单隐藏或显示 div 内容的最佳方法

javascript - 使用 CSS/JS/JQuery 自定义单选按钮

javascript - 日期选择器和 Laravel 5