Created
May 6, 2019 03:44
-
-
Save mattsan/aa3064c8bbcca10983ff72e47eb500e7 to your computer and use it in GitHub Desktop.
クリックした場所にDOMが移動する (Phoenix + Stimulusjs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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