Finding character position in strings, JS

Description: When I say find the position of a character in a string, I mean find how many pixels from the left and top sides of the screen a character has.

Should I surround the character in the span tag and find where this span tag is? Or is there a better way to do this with pure JS?

+3


source to share


1 answer


The choice you propose is the best solution I suggest. Unless you want to manipulate the character widths and so on. It depends on the font family and contains the letter.

A great way might be to implement a function to dynamically generate the hidden DOM div once for every character you want to dodge. It calculates the width of each one and adds to the variable. This will be the offset value. You understand?



I think its implementation is worth your professionalism and is adapted to your scenario as we don't have an example :) I give you something like pseudocode , which I mean if it helps:

function calculate_offset () {
var offset;
var position = element.positionX;
   for i in 0 to string.indexYouWant 
       do 
          variable = get_string[i]_value;
          generate_div(variable);
          offset += calculate_div_width();
   end for  

return position+offset;
}

      

0


source







All Articles