Autoscoping (#103)
* Add autoscoping with generated demo * Remove manual demo from all snippets * Add JavaScript inside IIFE * Align mouse-cursor-gradient-tracking.md to demo code * Match snippets to demo * Update CONTRIBUTING.md * Create reusable function for code extraction
This commit is contained in:
@ -54,8 +54,8 @@ A hover effect where the gradient follows the mouse cursor.
|
||||
```js
|
||||
var btn = document.querySelector('.mouse-cursor-gradient-tracking')
|
||||
btn.onmousemove = function(e) {
|
||||
var x = e.pageX - btn.offsetLeft
|
||||
var y = e.pageY - btn.offsetTop
|
||||
var x = e.pageX - btn.offsetLeft - btn.offsetParent.offsetLeft
|
||||
var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
|
||||
btn.style.setProperty('--x', x + 'px')
|
||||
btn.style.setProperty('--y', y + 'px')
|
||||
}
|
||||
@ -63,73 +63,10 @@ btn.onmousemove = function(e) {
|
||||
|
||||
#### Demo
|
||||
|
||||
<div class="snippet-demo">
|
||||
<button class="snippet-demo__mouse-cursor-gradient-tracking">
|
||||
<span>Hover me</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.snippet-demo__mouse-cursor-gradient-tracking {
|
||||
position: relative;
|
||||
background: #7983ff;
|
||||
padding: 0.5rem 1rem;
|
||||
font-size: 1.2rem;
|
||||
border: none;
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
outline: none;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.snippet-demo__mouse-cursor-gradient-tracking span {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.snippet-demo__mouse-cursor-gradient-tracking::before {
|
||||
--size: 0;
|
||||
content: '';
|
||||
position: absolute;
|
||||
left: var(--x);
|
||||
top: var(--y);
|
||||
width: var(--size);
|
||||
height: var(--size);
|
||||
background: radial-gradient(circle closest-side, aqua, rgba(0,255,255,0.0001));
|
||||
transform: translate(-50%, -50%);
|
||||
transition: width .2s ease, height .2s ease;
|
||||
}
|
||||
|
||||
.snippet-demo__mouse-cursor-gradient-tracking:hover::before {
|
||||
--size: 200px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
;(function () {
|
||||
var btn = document.querySelector('.snippet-demo__mouse-cursor-gradient-tracking')
|
||||
btn.onmousemove = function (e) {
|
||||
var x = e.pageX - btn.offsetLeft - btn.offsetParent.offsetLeft
|
||||
var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
|
||||
btn.style.setProperty('--x', x + 'px')
|
||||
btn.style.setProperty('--y', y + 'px')
|
||||
}
|
||||
})()
|
||||
</script>
|
||||
|
||||
#### Explanation
|
||||
|
||||
_TODO_
|
||||
|
||||
**Note!**
|
||||
|
||||
If the element's parent has a positioning context (`position: relative`), you will need to subtract
|
||||
its offsets as well.
|
||||
|
||||
```js
|
||||
var x = e.pageX - btn.offsetLeft - btn.offsetParent.offsetLeft
|
||||
var y = e.pageY - btn.offsetTop - btn.offsetParent.offsetTop
|
||||
```
|
||||
|
||||
#### Browser support
|
||||
|
||||
<div class="snippet__requires-javascript">Requires JavaScript</div>
|
||||
|
||||
Reference in New Issue
Block a user