{"id":3298,"date":"2019-11-26T18:07:00","date_gmt":"2019-11-26T18:07:00","guid":{"rendered":"https:\/\/azoora.com\/blog\/?p=3298"},"modified":"2019-11-27T18:19:09","modified_gmt":"2019-11-27T18:19:09","slug":"directional-link-effects-with-css-variables","status":"publish","type":"post","link":"https:\/\/azoora.com\/blog\/code\/directional-link-effects-with-css-variables\/","title":{"rendered":"Directional link effects with CSS variables"},"content":{"rendered":"\n<p>We really like the <strong>CSS variables<\/strong> and <a rel=\"noreferrer noopener\" href=\"https:\/\/medium.com\/@electerious\/parallax-scrolling-with-js-controlled-css-variables-63cfe96820c7\" target=\"_blank\">what we can do with them<\/a>. Changing them in JS is like using useState in <strong>React<\/strong>: The browser automatically re-renders the necessary parts. This allows you to build powerful effects in a few lines of code. My favourite is to track the position of the mouse to build directional cursor effects. Believe me: It\u2019s a small detail, but you can\u2019t stop hovering once you\u2019ve seen it.<\/p>\n\n\n\n<p>This tutorial here, explains the little magic behind the hover effect we\u2019ve found on the <a rel=\"noreferrer noopener\" href=\"https:\/\/ackee.electerious.com\/\" target=\"_blank\">menu of the Ackee site<\/a>.<\/p>\n\n\n\n<h1 id=\"bb78\">Track the position<\/h1>\n\n\n\n<p>We need to know the position of the cursor to build a directional effect. This can be done in JS.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.querySelectorAll('.underline').forEach((elem) =&gt; {\n\n\telem.onmouseenter =\n\telem.onmouseleave = (e) =&gt; {\n\n\t\tconst x = e.pageX - elem.offsetLeft\n\t\telem.style.setProperty('--x', `${ x }px`)\n\n\t}\n\n})<\/pre>\n\n\n\n<p>The CSS variable <code>--x<\/code> now contains the position of the mouse relative to the element. It will update whenever the mouse enters and leaves the element.<\/p>\n\n\n\n<p>Reality however showed me that it looks a little bit weird when <code>x<\/code> is near, but not exactly left (<code>0<\/code>) or right (<code>elem.clientWidth<\/code>). Let\u2019s fix this by adding a tolerance.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">document.querySelectorAll('.underline').forEach((elem) =&gt; {\n\n\telem.onmouseenter =\n\telem.onmouseleave = (e) =&gt; {\n\n\t\tconst tolerance = 10\n\n\t\tconst left = 0\n\t\tconst right = elem.clientWidth\n\n\t\tlet x = e.pageX - elem.offsetLeft\n\n\t\tif (x - tolerance &lt; left) x = left\n\t\tif (x + tolerance &gt; right) x = right\n\n\t\telem.style.setProperty('--x', `${ x }px`)\n\n\t}\n\n})<\/pre>\n\n\n\n<h1 id=\"3088\">Styling:<\/h1>\n\n\n\n<p><code>text-decoration: underline<\/code> can\u2019t be animated. We\u2019re therefore adding a line below the element using an <code>::after<\/code> pseudo element.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.underline {\n\n\tposition: relative;\n\tcolor: rgba(255, 255, 255, .7); \/\/ 1\n\ttext-decoration: none; \/\/ 1\n\ttransition: color .3s cubic-bezier(.51, .92, .24, 1);\n\n\t\/\/ 2\n\t&amp;::after {\n\t\tcontent: '';\n\t\tposition: absolute;\n\t\tleft: 0;\n\t\tright: 0;\n\t\ttop: 100%;\n\t\theight: 2px;\n\t\tbackground: linear-gradient(135deg, rgb(115, 250, 200), rgb(0, 190, 225));\n\t\ttransform: scaleX(var(--scale, 0)); \/\/ 3\n\t\ttransform-origin: var(--x) 50%; \/\/ 4\n\t\ttransition: transform .3s cubic-bezier(.51, .92, .24, 1);\n\t}\n\n\t&amp;:hover {\n\t\tcolor: white;\n\t}\n\n\t&amp;:hover::after {\n\t\t--scale: 1; \/\/5\n\t}\n\n}<\/pre>\n\n\n\n<ol><li>Remove the default styling of the link<\/li><li>Add a custom underline<\/li><li>Use <code>--scale<\/code> to show and hide the underline (starting with <code>0<\/code> = hidden)<\/li><li>Use <code>--x<\/code> to define the origin of the scale transform (the position of the mouse)<\/li><li>Show the underline by changing the <code>--scale<\/code> variable<\/li><\/ol>\n\n\n\n<h1 id=\"c379\">Conclusion:<\/h1>\n\n\n\n<p>That\u2019s it already. A few lines of code and hovering links makes fun, again. Add the missing HTML and enjoy:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-codepen\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"Direction aware hover effect\" id=\"cp_embed_xeJPbB\" src=\"https:\/\/codepen.io\/electerious\/embed\/preview\/xeJPbB?height=300&amp;slug-hash=xeJPbB&amp;default-tabs=css,result&amp;host=https:\/\/codepen.io\" scrolling=\"no\" frameborder=\"0\" height=\"300\" allowtransparency=\"true\" class=\"cp_embed_iframe\" style=\"width: 100%; overflow: hidden;\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>We really like the CSS variables and what we can do with them. Changing them in JS is like using useState in React: The browser automatically re-renders the necessary parts. This allows you to build powerful effects in a few lines of code. My favourite is to track the position of the mouse to build [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":3299,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[4,145,12,168],"tags":[25,94,146,136,116,87],"jetpack_featured_media_url":"https:\/\/azoora.com\/blog\/wp-content\/uploads\/2019\/11\/directional-link-effects-css.gif","jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/p7FQPL-Rc","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/3298"}],"collection":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/comments?post=3298"}],"version-history":[{"count":2,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/3298\/revisions"}],"predecessor-version":[{"id":3414,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/posts\/3298\/revisions\/3414"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media\/3299"}],"wp:attachment":[{"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/media?parent=3298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/categories?post=3298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/azoora.com\/blog\/wp-json\/wp\/v2\/tags?post=3298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}