3

我正在使用 document.body.getBoundingClientRect().right 来查找顶部导航中的所有元素都看不见,以便隐藏它们并将它们放在“更多”下拉列表下。但该功能似乎在 safari 中不起作用。该功能是否有任何替代方法,或者有什么方法可以让它在 Safari 中修复?

var windowRightOffset = document.body.getBoundingClientRect().right,
        elementHiddenFlag = false;

    $(".headerNav").find("li").each(function() {
        if ($(this).className !== 'more') {
            var elemRightOffset = $(this).find("a")[0].getBoundingClientRect().right;
            if (elemRightOffset > windowRightOffset) {
                $(this).hide();
                elementHiddenFlag = true;
                $(".more .moreNavItems-content").append($(this).html());
            }
        }
    });
4

2 回答 2

2

由于您使用的是 jQuery,您可以查看 jQuery 中的位置偏移函数。

要使用 jQuery 替换您的代码,您将执行以下操作:

var aTag = $(this).find("a")[0];
var left = aTag.offset().left;
var width = aTag.find("a")[0].width();

var aTagRightOffset = width + left;
于 2017-08-02T14:24:55.583 回答
1

好了,走吧 :-)

function getBounding(name,index){
    this.ele = document.querySelectorAll(name)[index];
    this.y= this.ele.offsetTop;
    this.x= this.ele.offsetLeft;
    this.coordinates();
  }

  getBounding.prototype.coordinates = function(){
    if( this.ele.localName != "body"){
      this.ele = this.ele.offsetParent;
      this.y += this.ele.offsetTop;
      this.x += this.ele.offsetLeft;
      this.coordinates();
    } else {
      return({x:this.x,y:this.y});
    }
  }

新的 getBounding(".-img",0)

于 2019-06-03T13:48:03.263 回答