[英]How to get shape / polygon / bounding path / frame of element in HTML?
In jQuery, zepto.js
etc you can say 在jQuery,
zepto.js
等你可以说
var position = $('a:first').position();
But the a-tag can span over several lines with a shape more complex than a simple rectangle (position and size). 但是a-tag可以跨越多条线,其形状比简单的矩形(位置和大小)更复杂。 What I would like to get is the path of the element.
我想得到的是元素的路径。
Example 例
The pipes in this examples is meant to illude |||||||
|||||| |||| || ||||||| an a-tag spanning over two lines.
Consisting of two different rects OR 8 different points.
So, is there any way to get the polygon of any element? 那么,有没有办法获得任何元素的多边形?
http://jsfiddle.net/mattdlockyer/EEVV2/ http://jsfiddle.net/mattdlockyer/EEVV2/
This will give you the bounding points of each line of a link. 这将为您提供链接的每一行的边界点。
Since building a poly will depend on the shape, I didn't really have time to get into this, but I think this is a reasonable starting point. 由于建造聚合物将取决于形状,我没有时间进入这个,但我认为这是一个合理的起点。
Hope it helps! 希望能帮助到你!
$(document).ready(function () {
var words = $('a').text().split(' '); //break the link's words into array
for (var i = 0; i < words.length; ++i) {
words[i] = '<span>' + words[i] + '</span> '; //wrap each word in a span
}
words.join(' '); //join the words
$('a').html(words); //replace the link html with the words wrapped in spans
var points = []; //to store our points
var lastIndex = $('a span').length - 1; //so we know we've hit the last point
var first; //keep track of first point
$('a span').each(function (i) {
var pos = $(this).position();
if (i > 0) {
if (i != lastIndex) {
if (points[points.length - 1].top < pos.top) { //have we hit a new line?
var newPos = $($('a span')[i - 1]).position();
if (newPos.left != first.left) { //check if we are on the right side of the object
newPos.left += $($('a span')[i - 1]).width(); //add the width of the word
points.push(newPos); //push this point
} else {
points.push(newPos); //we are on the left side, push the point without the width of the word
}
//$($('a span')[i - 1]).addClass('red'); //useful for debugging
}
} else {
var bottomLineHeight = $(this).height();
pos.left += $(this).width(); //add width to last point
pos.top += bottomLineHeight; //add height to last point, push later
points[points.length - 1].top += bottomLineHeight; //add height to second to last point
points.push(pos); //push last point
}
} else {
points.push(pos); //push first point
first = pos; //keep track of topLeft point (first)
}
});
//for printing out points
var html = '';
$(points).each(function (i, obj) {
html += '<p>(' + obj.left + ', ' + obj.top + ')';
});
$('#result').html(html);
});//ready
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.