Last active
March 2, 2019 08:00
-
-
Save niconico25/d923d0765c293937bd9bb596cb5ea84d to your computer and use it in GitHub Desktop.
はてなブログでマーカーが引けるようになります。
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
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.7/dist/vue.js"></script> | |
<script> | |
// DOM が描画されるまで待つ | |
setTimeout(vue, 1000); | |
function vue() { | |
new Vue({ | |
el: '#container', | |
components: { | |
hl: highlight(), | |
} | |
}); | |
} | |
// Just return a Vue component. | |
function highlight() { | |
// 2) configuration | |
const startLineOffset = -100; | |
class Style { | |
constructor(highlighter) { | |
const color = highlighter.color | |
this['background'] = `linear-gradient(${color}, ${color})`; | |
this['background-size'] = '0% 45%'; | |
this['background-position'] = '0% 100%'; | |
this['background-repeat'] = 'no-repeat'; | |
this['transition-delay'] = '1s'; | |
this['transition-duration'] = '2.5s'; | |
} | |
}; | |
// 3) Return a Vue component. | |
return { | |
template: | |
'<span v-bind:style="style">' + | |
'<slot></slot>' + | |
'</span>', | |
props: ['color'], | |
data: function() { | |
return { | |
style: new Style(this), | |
}; | |
}, | |
mounted: function() { | |
window.addEventListener('scroll', this.scroll); | |
}, | |
methods: { | |
scroll: function() { | |
if (this.startLine() < this.domRectTop()){ | |
this.style['background-size'] = ' 0% 45%'; | |
} else { | |
this.style['background-size'] = '100% 45%'; | |
} | |
}, | |
startLine: function() { | |
return window.innerHeight + startLineOffset; | |
}, | |
domRectTop: function() { | |
return this.$el.getBoundingClientRect().top; | |
}, | |
}, | |
}; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment