我发现这对我的博客来说是一件有趣的事情,并犯了一些错误。如果有人可以用它制作一个 jquery 插件?!
图像和文本应该在一个 div 中。看到它工作here。
1.单词没有坐标,所以你必须用一个html标签来包装每个单词。这增加了很多文字!!!
var text=$('div#text').text();
var arr=text.split(" ");
for(var i=0;i<arr.length;i++){
arr[i]="<span id='t'>"+arr[i]+"</span>";
}
text=arr.join(" ");
$('div#text').html(function(){return text;});
2.放置您的图像。
3.获取图片的div的坐标和大小,并声明一些vars。
var temp=$("div#img");
var pos=temp.offset();
var imgleft=parseInt(pos.left);
var imgtop=parseInt(pos.top);
var imgwidth=temp.width();
var imgheight=temp.height();
var latesttop=$("div#text").offset().top;
var spanleft,spantop,spanwidth,spanheight,latestleft,latestwidth;
4.遍历每个单词标签。找到突出到图像中的单词。计算before单词结尾到图片右边缘的距离,放置一个特定宽度的div作为占位符。
$("span#t").each(function(index){
pos=$(this).offset();
spanleft=parseInt(pos.left);
spantop=parseInt(pos.top);
spanwidth=$(this).width();
spanheight=$(this).height();
if(spantop+spanheight>imgtop
&& spantop<imgtop+imgheight
&& spanleft+spanwidth>imgleft
&& spanleft<imgleft+imgwidth){
if(latesttop!=spantop){
latestleft=0;
latestwidth=0;
}
var spacewidth=15+imgwidth+imgleft-latestleft-latestwidth;
$(this).before("<div id='t' style='display:inline-block;width:"+spacewidth+"px'></div>");
}
latesttop=spantop;
latestleft=spanleft;
latestwidth=spanwidth;
});
5.完成后从自动换行中清除文本。
$('span#t').replaceWith(function(){
return $(this).contents();
});