Skip to content

Instantly share code, notes, and snippets.

@antfu
Last active August 24, 2016 01:43
Show Gist options
  • Save antfu/6493f836155ef4ad322ff5b33bb431c7 to your computer and use it in GitHub Desktop.
Save antfu/6493f836155ef4ad322ff5b33bb431c7 to your computer and use it in GitHub Desktop.
[JS] Vue directive for smooth background-image loading
Vue.directive('background-image',{
update: function(url) {
var el = $(this.el);
el.css('transition','opacity 0.5s ease-in').css('opacity',0);
var img = $('<img>', {
src: url
}).hide().on('load',function() {
$(this).remove();
el.css('background-image', 'url("'+url+'")').css('opacity',1);
}).appendTo(el);
}
});
<div v-background-image="'/static/example.jpg'"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment