Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Phew! I finished!</p> <p>JS :</p> <pre><code>var x=0,y=0;//Here are the given X and Y, you can change them var idClosest;//Id of the nearest anchor var smallestIndex; var couplesXY=[]; var allAnchors; var html=document.getElementsByTagName("html")[0]; html.style.width="3000px";//You can change 3000, it's to make the possibility of horizontal scroll html.style.height="3000px";//Here too function random(min,max) { var nb=min+(max+1-min)*Math.random(); return Math.floor(nb); } function left(obj)//A remixed function of this site http://www.quirksmode.org/js/findpos.html { if(obj.style.position=="absolute")return parseInt(obj.style.left); var posX=0; if(!obj.offsetParent)return; do posX+=obj.offsetLeft; while(obj=obj.offsetParent); return posX; } function top(obj) { if(obj.style.position=="absolute")return parseInt(obj.style.top); var posY=0; if(!obj.offsetParent)return; do posY+=obj.offsetTop; while(obj=obj.offsetParent); return posY; } function generateRandomAnchors()//Just for the exemple, you can delete the function if you have already anchors { for(var a=0;a&lt;50;a++)//You can change 50 { var anchor=document.createElement("a"); anchor.style.position="absolute"; anchor.style.width=random(0,100)+"px";//You can change 100 anchor.style.height=random(0,100)+"px";//You can change 100 anchor.style.left=random(0,3000-parseInt(anchor.style.width))+"px";//If you changed 3000 from anchor.style.top=random(0,3000-parseInt(anchor.style.height))+"px";//the top, change it here anchor.style.backgroundColor="black"; anchor.id="Anchor"+a; document.body.appendChild(anchor); } } function getAllAnchors() { allAnchors=document.getElementsByTagName("a"); for(var a=0;a&lt;allAnchors.length;a++) { couplesXY[a]=[]; couplesXY[a][0]=left(allAnchors[a]); couplesXY[a][1]=top(allAnchors[a]); } } function findClosestAnchor() { var distances=[]; for(var a=0;a&lt;couplesXY.length;a++)distances.push(Math.pow((x-couplesXY[a][0]),2)+Math.pow((y-couplesXY[a][1]),2));//Math formula to get the distance from A to B (http://euler.ac-versailles.fr/baseeuler/lexique/notion.jsp?id=122). I removed the square root not to slow down the calculations var smallest=distances[0]; smallestIndex=0; for(var a=1;a&lt;distances.length;a++)if(smallest&gt;distances[a]) { smallest=distances[a]; smallestIndex=a; } idClosest=allAnchors[smallestIndex].id; alert(idClosest); } function jumpToIt() { window.scrollTo(couplesXY[smallestIndex][0],couplesXY[smallestIndex][1]); allAnchors[smallestIndex].style.backgroundColor="red";//Color it to see it } generateRandomAnchors(); getAllAnchors(); findClosestAnchor(); jumpToIt(); </code></pre> <p>Fiddle : <a href="http://jsfiddle.net/W8LBs/2" rel="nofollow">http://jsfiddle.net/W8LBs/2</a></p> <p>PS : If you open this fiddle on a smartphone, it doesn't work (I don't know why) but if you copy this code in a sample on a smartphone, it works (but you must specify the <code>&lt;html&gt;</code> and the <code>&lt;body&gt;</code> section).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload