我找不到传递给'链接的元素的高度或宽度。功能。我需要它,因为我想计算鼠标点击附加到元素的图像的位置。 offsetHeight,offsetWidth,clientHeight和clientWidth都是未定义的,x和y的位置也是如此。 getBoundingClientRect抛出一个'而不是函数'错误。在模板中设置高度和宽度属性不会改变任何内容。
这是一个说明问题的简单指令:
`<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- TemplateBeginEditable name="doctitle" --> <title>Fine Antique Clocks, Barometers & Marine Chronometers - Archives</title> <!-- TemplateEndEditable --> <!-- Bootstrap --> <link rel="stylesheet" href="../css/bootstrap.css"> <style type="text/css"> body { width: 100%; background-image: url(../images/ancient_clock_2048_pl.jpg); background-repeat: no-repeat; background-attachment: fixed; background-color: rgba(229,207,111,0.7); background-size: cover; } </style> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> <!-- TemplateBeginEditable name="head" --> <!-- TemplateEndEditable --> </head> <body> <nav> <div class="container"> <div class="bada"><a href="http://www.bada.org/provenart/dealer.cgi?d_id=67&c_id=193" target="_blank"><img src="../images/BADA-logo-colour_round.jpg" class="img-circle img-responsive" alt="British Antique Dealers Association"></a></div> <!-- Brand and toggle get grouped for better mobile display --><br> <br> <img src="/images/patriccapon_antique_clocks.jpg" class="img-rounded img-responsive" alt="Placeholder image"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> </div> <a class="navbar-brand" href="../index.html"><span class="glyphicon glyphicon-home" aria-hidden="true"></span> </a> </div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="../antique_clocks_stock_01.html">Stock <span class="sr-only">(current)</span></a> </li> <li class="active"><a href="../antique_clocks_archives_01.html">Archives <span class="sr-only">(current)</span></a> </li> <li class="active"><a href="../antique_clocks_services.html">Services <span class="sr-only">(current)</span></a> </li> <li><a href="../antique_clocks_news.html">News</a> </li> </ul> <ul class="nav navbar-nav navbar-right hidden-sm"> <li><a href="../antique_clocks_contact_us.html">Contact Us</a> </li> <ul class="nav navbar-nav navbar-right hidden-sm"> <li><a href="../antique_clocks_links.html">About Us</a> </li> <ul class="nav navbar-nav navbar-right hidden-sm"> <li><a href="../antique_clocks_links.html">Links</a> </li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">Dropdown <span class="caret"></span></a> </li></ul> </ul></ul></div> <!-- /.navbar-collapse --> <!-- /.container-fluid --> </nav><br>
在收到//OLD
var ngnDirectives = angular.module('ngnDirectives');
ngnDirectives.directive('positionTest', function() {
function link(scope, element, attrs) {
console.log("POSITION");
var position = element.position();
for(p in position) {
console.log(p + ", " + element[p]);
}
console.log("END POSITION");
var offset = element.offset();
console.log("element.offset x, y: " + offset.left + ", " + offset.top);
console.log("offsetWidth, offsetHeight: " + element.offsetWidth + ", " + element.offsetHeight);
console.log("clientWidth, clientHeight: " + element.clientWidth + ", " + element.clientHeight);
//get BoundingClientRect throws 'not a function' error
console.log("getBoundingClientRect: " + element.getBoundingClientRect());
}
return {
scope: {},
restrict: 'E',
template: '<div></div>',
link: link
};
});
是原始DOM元素的评论后,我发现element[0]
,offsetWidth
,offsetHeight
和clientWidth
仍然不在返回我理解的值。我更改了代码以使用clientHeight
,并发现它没有返回正确的&#39;底部&#39;对于这个数字。这是代码:
getBoundingClientRect
答案 0 :(得分:0)
问题在于我没有从正确的元素获得高度和宽度。在进入链接函数时,我将svg标记附加到元素。 svg标签具有宽度和高度,并且在该标签上使用getClientBoundingRect。但是,元素[0]是来自指令&#39;模板的标签:&#39;定义(空div)。因此,在模板中开始指令的元素如下所示:
function isScrolledIntoView(elem) {
if (elem.length > 0) {
docViewTop = $(window).scrollTop();
docViewBottom = docViewTop + $(window).height();
elemTop = $(elem).offset().top;
return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
}
}
if (isScrolledIntoView($('.wrapper.site-section:nth-child(1)')) === true) {
if ($('.desktop').length > 0) {
$('.sidebar li').removeClass('showAdvertisement');
$('.sidebar li:nth-child(4)').addClass('showAdvertisement');
}
}
if (isScrolledIntoView($('.wrapper .site-section:nth-child(2)')) === true) {
if ($('.desktop').length > 0) {
$('.sidebar li').removeClass('showAdvertisement');
$('.sidebar li:nth-child(5)').addClass('showAdvertisement');
}
}
if (isScrolledIntoView($('.wrapper .site-section:nth-child(3)')) === true) {
if ($('.desktop').length > 0) {
$('.sidebar li').removeClass('showAdvertisement');
$('.sidebar li:nth-child(6)').addClass('showAdvertisement');
}
}
将svg标记附加到元素会导致显示svg标记的字符串文字表示,我可以修复它,但它也可以简单地选择传入链接函数的元素的svg子元素。