|
17 | 17 | </title> |
18 | 18 | <style> |
19 | 19 | .bubble-box{ |
20 | | - max-width: 20rem; /* You are likely to change it */ |
21 | 20 | position: absolute; |
22 | | - right: -999px; |
23 | 21 | background: white; |
24 | 22 | border-radius: 4px; |
25 | 23 | padding: 4px; |
26 | 24 | word-break: break-all; |
27 | 25 | word-wrap: break-word; |
28 | 26 | box-shadow: 5px 5px 5px 3px rgba(0,0,0,0.2); |
| 27 | + } |
| 28 | + .bubble-box.adaptive-width{ /*complicated*/ |
| 29 | + max-width: 20rem; /* change to value you like */ |
| 30 | + } |
| 31 | + .bubble-box.fixed-width{ /*easy*/ |
| 32 | + width: 20rem; /* change to value you like */ |
29 | 33 | transform: translate(-50%, calc(-100% - 4px)); |
30 | 34 | } |
31 | 35 | .bubble-box::after{ |
|
105 | 109 | }; |
106 | 110 |
|
107 | 111 | divInf = document.createElement("div"); |
108 | | - divInf.className = 'bubble-box'; |
| 112 | + divInf.classList.add('bubble-box', 'adaptive-width'); // or 'fixed-width' |
109 | 113 | const bodyStyle = getComputedStyle(document.body); |
110 | 114 | if (bodyStyle.position === "static") { |
111 | 115 | /** |
112 | 116 | * 'convertToPageCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the document. |
113 | 117 | * Then we can place a div element according to the converted coordinate. |
114 | 118 | */ |
115 | | - const convertedP1 = scanner.convertToPageCoordinates(p1); |
116 | | - const convertedP2 = scanner.convertToPageCoordinates(p2); |
117 | | - Object.assign(divInf.style, { |
118 | | - left: `${(convertedP1.x + convertedP2.x) / 2}px`, |
119 | | - top: `${convertedP1.y}px`, |
120 | | - }); |
121 | | - } else { // if you set body `position` as `relative`, `absolute`, things can get complicated |
| 119 | + const pageP1 = scanner.convertToPageCoordinates(p1); |
| 120 | + const pageP2 = scanner.convertToPageCoordinates(p2); |
| 121 | + const pageMidX = (pageP1.x + pageP2.x) / 2; |
| 122 | + divInf.style.top = `${pageP1.y}px`; |
| 123 | + if(divInf.classList.contains('fixed-width')){ |
| 124 | + // fixed-width, easy |
| 125 | + divInf.style.left = `${pageMidX}px`; |
| 126 | + }else if(pageMidX < document.documentElement.clientWidth / 2){ |
| 127 | + // adaptive-width, near left |
| 128 | + divInf.style.left = `${pageMidX}px`; |
| 129 | + divInf.style.transform = `translate(-50%, calc(-100% - 4px))`; |
| 130 | + }else{ |
| 131 | + // adaptive-width, near right |
| 132 | + divInf.style.right = `${document.documentElement.clientWidth - pageMidX}px`; |
| 133 | + divInf.style.transform = `translate(50%, calc(-100% - 4px))`; |
| 134 | + } |
| 135 | + } else { // if you set body `position` as `relative`, `absolute`, and so on, things can get complicated. |
122 | 136 | /** |
123 | 137 | * 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport. |
124 | 138 | * Then we can place a div element according to the converted coordinate. |
125 | 139 | */ |
126 | | - const convertedP1 = scanner.convertToClientCoordinates(p1); |
127 | | - const convertedP2 = scanner.convertToClientCoordinates(p2); |
| 140 | + const clientP1 = scanner.convertToClientCoordinates(p1); |
| 141 | + const clientP2 = scanner.convertToClientCoordinates(p2); |
| 142 | + const clientMidX = (clientP1.x + clientP2.x) / 2; |
128 | 143 | const bodyRect = document.body.getBoundingClientRect(); |
129 | | - const bodyBorderLeft = parseFloat(bodyStyle.borderLeftWidth); |
130 | | - const bodyBorderTop = parseFloat(bodyStyle.borderTopWidth); |
131 | | - Object.assign(divInf.style, { |
132 | | - left: `${(convertedP1.x + convertedP2.x) / 2 - bodyRect.left - bodyBorderLeft}px`, |
133 | | - top: `${convertedP1.y - bodyRect.top - bodyBorderTop}px`, |
134 | | - }); |
| 144 | + divInf.style.top = `${clientP1.y - bodyRect.top - parseFloat(bodyStyle.borderTopWidth)}px`; |
| 145 | + if(divInf.classList.contains('fixed-width')){ |
| 146 | + // fixed-width, easy |
| 147 | + divInf.style.left = `${clientMidX - bodyRect.left - parseFloat(bodyStyle.borderLeftWidth)}px`; |
| 148 | + }else if(clientMidX < bodyRect.left + bodyRect.width / 2){ |
| 149 | + // adaptive-width, near left |
| 150 | + divInf.style.left = `${clientMidX - bodyRect.left - parseFloat(bodyStyle.borderLeftWidth)}px`; |
| 151 | + divInf.style.transform = `translate(-50%, calc(-100% - 4px))`; |
| 152 | + }else{ |
| 153 | + // adaptive-width, near right |
| 154 | + divInf.style.right = `${bodyRect.left + bodyRect.width - clientMidX}px`; |
| 155 | + divInf.style.transform = `translate(50%, calc(-100% - 4px))`; |
| 156 | + } |
135 | 157 | } |
136 | 158 | divInf.innerText = result.barcodeText; |
137 | 159 | divInfContainers.append(divInf); |
|
0 commit comments