使用原型JS的浮动小部件/横幅

Floating widget/banner using the prototype JS

本文关键字:小部 横幅 原型 JS      更新时间:2023-09-26

我使用jQuery中的原始文件创建了一个脚本:http://terkel.jp/demo/jquery-floating-widget-plugin.html

但是当输入到原型JS时,它不能正常工作。严格来说,它没有正确计算上下高度。

有什么想法吗?我附上了我的代码和链接来查看活动脚本。

联机问题:http://1.delnik20.cz/

原型:

document.observe('dom:loaded', function() {
    Element.addMethods({
        floatingWidget: function(el, classes) {
            var $this       = $(el),
            $parent         = $this.getOffsetParent(),
            $window         = $(window),
            top             = $this.positionedOffset().top - parseFloat($this.getStyle('marginTop').replace(/auto/, 0)),
            bottom          = $parent.positionedOffset().top + $parent.getHeight() - $this.getHeight();
            if ($parent.getHeight() > $this.getHeight()) {
                Event.observe($window, 'scroll', function (e) {
                    var y = document.viewport.getScrollOffsets().top;
                    if (y > top) {
                        $this.addClassName(classes.floatingClass);
                        if (y > bottom) {
                            $this.removeClassName(classes.floatingClass).addClassName(classes.pinnedBottomClass);
                        } else {
                            $this.removeClassName(classes.pinnedBottomClass);
                        }
                    } else {
                        $this.removeClassName(classes.floatingClass);
                    }
                });
            }
            return el;
        }
    });
    $('floating-widget').floatingWidget({
        floatingClass: 'floating',
        pinnedBottomClass: 'pinned-bottom'
    });
});

Prototypejs等效于jQuery的offset()cumulativeOffset(),等效于outerHeight()measure('margin-box-height')(可以使用Element.Layout对象进行优化)。因此,您的代码应该看起来像:

floatingWidget: function(el, classes) {
    var $this        = $(el),
        $parent      = $this.getOffsetParent(),
        $window      = $(window),
        top          = $this.cumulativeOffset().top - $this.measure('margin-top'),
        bottom       = $parent.cumulativeOffset().top + $parent.getHeight() - $this.measure('margin-box-height');
if ($parent.getHeight() > $this.measure('margin-box-height')) {
...