【问题标题】:Django Horizontal Scroll on Top of Changelist更改列表顶部的 Django 水平滚动
【发布时间】:2020-08-19 16:26:10
【问题描述】:

我遇到了与 mozman2 描述的 here 相同的问题:

“在我的 Django 更改列表中有很多列,这意味着列表底部有一个滚动条。是否可以让滚动条出现在顶部,这样我就不需要向下滚动了”

来自给定链接的解决方案似乎对 mozman2 有所帮助。但是,我无法重现它。因此我尝试从

复制粘贴代码

https://github.com/avianey/jqDoubleScroll#readme

特别是,我将此文件从存储库复制到 MyApp/static/admin/js/

jquery.doubleScroll.js

文件如下所示:

/*
 * @name DoubleScroll
 * @desc displays scroll bar on top and on the bottom of the div
 * @requires jQuery
 *
 * @author Pawel Suwala - http://suwala.eu/
 * @author Antoine Vianey - http://www.astek.fr/
 * @version 0.5 (11-11-2015)
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Usage:
 * https://github.com/avianey/jqDoubleScroll
 */
 (function( $ ) {

    jQuery.fn.doubleScroll = function(userOptions) {

        // Default options
        var options = {
            contentElement: undefined, // Widest element, if not specified first child element will be used
            scrollCss: {
                'overflow-x': 'auto',
                'overflow-y': 'hidden',
                'height': '20px'
            },
            contentCss: {
                'overflow-x': 'auto',
                'overflow-y': 'hidden'
            },
            onlyIfScroll: true, // top scrollbar is not shown if the bottom one is not present
            resetOnWindowResize: false, // recompute the top ScrollBar requirements when the window is resized
            timeToWaitForResize: 30 // wait for the last update event (usefull when browser fire resize event constantly during ressing)
        };

        $.extend(true, options, userOptions);

        // do not modify
        // internal stuff
        $.extend(options, {
            topScrollBarMarkup: '<div class="doubleScroll-scroll-wrapper"><div class="doubleScroll-scroll"></div></div>',
            topScrollBarWrapperSelector: '.doubleScroll-scroll-wrapper',
            topScrollBarInnerSelector: '.doubleScroll-scroll'
        });

        var _showScrollBar = function($self, options) {

            if (options.onlyIfScroll && $self.get(0).scrollWidth <= $self.width()) {
                // content doesn't scroll
                // remove any existing occurrence...
                $self.prev(options.topScrollBarWrapperSelector).remove();
                return;
            }

            // add div that will act as an upper scroll only if not already added to the DOM
            var $topScrollBar = $self.prev(options.topScrollBarWrapperSelector);

            if ($topScrollBar.length == 0) {

                // creating the scrollbar
                // added before in the DOM
                $topScrollBar = $(options.topScrollBarMarkup);
                $self.before($topScrollBar);

                // apply the css
                $topScrollBar.css(options.scrollCss);
                $(options.topScrollBarInnerSelector).css("height", "20px");
                $self.css(options.contentCss);

                var scrolling = false;

                // bind upper scroll to bottom scroll
                $topScrollBar.bind('scroll.doubleScroll', function() {
                    if (scrolling) {
                        scrolling = false;
                        return;
                    }
                    scrolling = true;
                    $self.scrollLeft($topScrollBar.scrollLeft());
                });

                // bind bottom scroll to upper scroll
                var selfScrollHandler = function() {
                    if (scrolling) {
                        scrolling = false;
                        return;
                    }
                    scrolling = true;
                    $topScrollBar.scrollLeft($self.scrollLeft());
                };
                $self.bind('scroll.doubleScroll', selfScrollHandler);
            }

            // find the content element (should be the widest one)
            var $contentElement;

            if (options.contentElement !== undefined && $self.find(options.contentElement).length !== 0) {
                $contentElement = $self.find(options.contentElement);
            } else {
                $contentElement = $self.find('>:first-child');
            }

            // set the width of the wrappers
            $(options.topScrollBarInnerSelector, $topScrollBar).width($contentElement.outerWidth());
            $topScrollBar.width($self.width());
            $topScrollBar.scrollLeft($self.scrollLeft());

        }

        return this.each(function() {

            var $self = $(this);

            _showScrollBar($self, options);

            // bind the resize handler
            // do it once
            if (options.resetOnWindowResize) {

                var id;
                var handler = function(e) {
                    _showScrollBar($self, options);
                };

                $(window).bind('resize.doubleScroll', function() {
                    // adding/removing/replacing the scrollbar might resize the window
                    // so the resizing flag will avoid the infinite loop here...
                    clearTimeout(id);
                    id = setTimeout(handler, options.timeToWaitForResize);
                });

            }

        });

    }

}( jQuery ));

然后我告诉 django 文件使用

class Media:
    js = (
    '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', # jquery
    'js/jquery.doubleScroll.js',       # project static folder
)

之后我使用了一个 collectstatic-command:

...

Copying '/MyApp/static/admin/js/jquery.doubleScroll.js'

...
1 static file copied to '/MyApp/static', 123 unmodified.

但是,顶部的水平滚动条不显示。

在github存储库中建议使用双滚动条

$(document).ready(function() {
   $('.double-scroll').doubleScroll();
});

我把这个放在哪里?我尝试在同一个 .js-File 上使用它而不是开始

(function( $ ) {
...
};

这也无济于事。

我想我错过了什么?

【问题讨论】:

    标签: javascript jquery django


    【解决方案1】:

    我通过了解 django - jquery 的语法解决了我的问题。 方法如下:

    我有 2 个新的 .js - 文件,一个只调用提供的双滚动功能 (calling.js) 和作者的 js 文件 (doubleScroll.js) 本身。

    在 admin.py 的 adminModel 中输入:

    class Media:
        js = ('//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js',
              'admin/js/doubleScroll.js',
              'admin/js/calling.js',)
    

    如果我理解正确的话,'//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js' - 链接是运行原生 jquery 所必需的。

    calling.js:

    django.jQuery(document).ready(function() {
    setTimeout(() => $('.results').doubleScroll({resetOnWindowResize: true}), 245);
    });
    

    doubleScroll.js:

    /*
     * @name DoubleScroll
     * @desc displays scroll bar on top and on the bottom of the div
     * @requires jQuery
     *
     * @author Pawel Suwala - http://suwala.eu/
     * @author Antoine Vianey - http://www.astek.fr/
     * @version 0.5 (11-11-2015)
     *
     * Dual licensed under the MIT and GPL licenses:
     * http://www.opensource.org/licenses/mit-license.php
     * http://www.gnu.org/licenses/gpl.html
     *
     * Usage:
     * https://github.com/avianey/jqDoubleScroll
     */
     (function( $ ) {
    
        jQuery.fn.doubleScroll = function(userOptions) {
    
            // Default options
            var options = {
                contentElement: undefined, // Widest element, if not specified first child element will be used
                scrollCss: {
                    'overflow-x': 'auto',
                    'overflow-y': 'hidden',
                    'height': '20px'
                },
                contentCss: {
                    'overflow-x': 'auto',
                    'overflow-y': 'hidden'
                },
                onlyIfScroll: true, // top scrollbar is not shown if the bottom one is not present
                resetOnWindowResize: false, // recompute the top ScrollBar requirements when the window is resized
                timeToWaitForResize: 30 // wait for the last update event (usefull when browser fire resize event constantly during ressing)
            };
    
            $.extend(true, options, userOptions);
    
            // do not modify
            // internal stuff
            $.extend(options, {
                topScrollBarMarkup: '<div class="doubleScroll-scroll-wrapper"><div class="doubleScroll-scroll"></div></div>',
                topScrollBarWrapperSelector: '.doubleScroll-scroll-wrapper',
                topScrollBarInnerSelector: '.doubleScroll-scroll'
            });
    
            var _showScrollBar = function($self, options) {
    
                if (options.onlyIfScroll && $self.get(0).scrollWidth <= $self.width()) {
                    // content doesn't scroll
                    // remove any existing occurrence...
                    $self.prev(options.topScrollBarWrapperSelector).remove();
                    return;
                }
    
                // add div that will act as an upper scroll only if not already added to the DOM
                var $topScrollBar = $self.prev(options.topScrollBarWrapperSelector);
    
                if ($topScrollBar.length == 0) {
    
                    // creating the scrollbar
                    // added before in the DOM
                    $topScrollBar = $(options.topScrollBarMarkup);
                    $self.before($topScrollBar);
    
                    // apply the css
                    $topScrollBar.css(options.scrollCss);
                    $(options.topScrollBarInnerSelector).css("height", "20px");
                    $self.css(options.contentCss);
    
                    var scrolling = false;
    
                    // bind upper scroll to bottom scroll
                    $topScrollBar.bind('scroll.doubleScroll', function() {
                        if (scrolling) {
                            scrolling = false;
                            return;
                        }
                        scrolling = true;
                        $self.scrollLeft($topScrollBar.scrollLeft());
                    });
    
                    // bind bottom scroll to upper scroll
                    var selfScrollHandler = function() {
                        if (scrolling) {
                            scrolling = false;
                            return;
                        }
                        scrolling = true;
                        $topScrollBar.scrollLeft($self.scrollLeft());
                    };
                    $self.bind('scroll.doubleScroll', selfScrollHandler);
                }
    
                // find the content element (should be the widest one)
                var $contentElement;
    
                if (options.contentElement !== undefined && $self.find(options.contentElement).length !== 0) {
                    $contentElement = $self.find(options.contentElement);
                } else {
                    $contentElement = $self.find('>:first-child');
                }
    
                // set the width of the wrappers
                $(options.topScrollBarInnerSelector, $topScrollBar).width($contentElement.outerWidth());
                $topScrollBar.width($self.width());
                $topScrollBar.scrollLeft($self.scrollLeft());
    
            }
    
            return this.each(function() {
    
                var $self = $(this);
    
                _showScrollBar($self, options);
    
                // bind the resize handler
                // do it once
                if (options.resetOnWindowResize) {
    
                    var id;
                    var handler = function(e) {
                        _showScrollBar($self, options);
                    };
    
                    $(window).bind('resize.doubleScroll', function() {
                        // adding/removing/replacing the scrollbar might resize the window
                        // so the resizing flag will avoid the infinite loop here...
                        clearTimeout(id);
                        id = setTimeout(handler, options.timeToWaitForResize);
                    });
    
                }
    
            });
    
        }
    
    }( jQuery ));
    

    别忘了跑

    collectstatic
    

    【讨论】:

      猜你喜欢
      • 2018-05-19
      • 1970-01-01
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多