Skip to content

Instantly share code, notes, and snippets.

@mattsan
Created May 6, 2019 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattsan/aa3064c8bbcca10983ff72e47eb500e7 to your computer and use it in GitHub Desktop.
Save mattsan/aa3064c8bbcca10983ff72e47eb500e7 to your computer and use it in GitHub Desktop.
クリックした場所にDOMが移動する (Phoenix + Stimulusjs)
.field {
background-color: #eee;
height: 90vh;
width: 100%;
}
.marker {
position: relative;
left: 10px;
height: 10px;
width: 10px;
display: inline-block;
background-color: black;
transition-duration: .3s;
transition-delay: 0s;
transition-timing-function: ease;
}
import { Controller } from 'stimulus'
export default class FieldController extends Controller {
static targets = [
'marker',
'posX'
]
move(event) {
const x = event.offsetX
const y = event.offsetY
this.posXTarget.textContent = `(${x}, ${y})`
this.markerTarget.style.left = `${x}px`
this.markerTarget.style.top = `${y}px`
}
}
<div data-controller="field">
<div class="field" data-action="click->field#move">
<span class="marker" data-target="field.marker"></span>
</div>
<div data-target="field.posX"></div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment