javascript - jQuery One Page Nav Plugin 溢出隐藏问题

标签 javascript jquery css smooth-scrolling

我在一个单页网站上工作(我正在使用 Travor Davis 的 jQuery One Page Nav 插件 http://github.com/davist11/jQuery-One-Page-Nav),我在左侧有一个固定菜单,在右侧有一些滚动内容。

不幸的是,我的主要内容 div 的高度是固定的,然后我使用 overflow hidden 属性来隐藏 div 之外的内容,但是由于 overflow hidden 插件无法工作并且内容不再滚动。

我该如何解决这个问题?

请查看下面的当前代码:

这是插件代码:

;(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.$win = $(window);
    this.sections = {};
    this.didScroll = false;
    this.$doc = $(document);
    this.docHeight = this.$doc.height();
};

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

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

        this.$nav = this.$elem.find(this.config.navItems);

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

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

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

        //Handle scroll changes
        this.bindInterval();

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

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

    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
            self.scrollTo(newLoc, 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);
                }
            }
        }
    },

    scrollTo: function(target, callback) {
        var offset = $(target).offset().top;

        $('html, body').animate({
            scrollTop: offset
        }, this.config.scrollSpeed, this.config.easing, callback);
    },

    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代码:

<div id="wrapper">
    <div id="container">
        <div id="left-col">
          <nav id="nav">
            <ul>
              <li class="active">
                <a href="#project">Project</a>
              </li>
              <li>
                <a href="#concept">Concept</a>
              </li>
              <li>
               <a href="#clients">Clients</a>
              </li>
              <li>
                <a href="#technical-specification">Technical<br>specification</a>
              </li>
              <li>
                <a href="#gallery">Gallery</a>
              </li>
          </ul>
      </div>
      <div id="right-col">
        <section id="project">
        </section>
        <section id="concept">
        </section>
        <section id="project">
        </section>
        <section id="clients">
    </section>
    <section id="technical-specification">
    </section>
        <section id="gallery">
    fgdgddg
    </section>
      </div>
 </div>

要查看正在运行的实际网站,请单击此处:

http://methlabtest2.altervista.org/EN/

提前致谢。

最佳答案

您添加了 overflow: hidden; 并且它正在做它应该做的事情( overflow hidden 并且不滚动)。

如果你想溢出滚动使用overflow: scroll;溢出-y;

关于javascript - jQuery One Page Nav Plugin 溢出隐藏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23524337/

相关文章:

javascript - 如何将单选按钮与输入文本字段绑定(bind)?

javascript - PNG 上的发光效果脉动发光效果

Javascript奇怪的比较结果

javascript - selenium webdriver 管理器更新 - npm

c# - 使用 jquery、通用处理程序和等待状态下载文件

CSS 样式不适用于具有类的元素

javascript - 如何通过 emscripten 在 C++ 和 javascript 之间传递字符串

javascript - 为什么在使用 JQuery 时,当我悬停时,没有流畅的动画?

javascript - Highcharts 显示在多个同名的 div 中

css - 带有阿拉伯语的多语言 UTF-8 网站