I found an interesting thing for my blog and made some traces and mistakes. if anyone can make a jquery plugin out of it ?! Images and text must be inside the div. See how it works here .
1. Words have no coordinates, so you need to wrap each word with an html tag. This greatly increases the text.
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. Put your image. 3. Set the coordinates and size of the div for the image and declare a few 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. Go through each word tag. Find the word that appears in the image. Calculate the distance from the end of the word to the right edge of the image and place a div with a certain width as the place holder.
$("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.Clean text from word wrappers upon completion.
$('span#t').replaceWith(function(){ return $(this).contents(); });
source share