Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="description">
  <h1>Auto-updating Background Image (using Vue.js and Axios)</h1>
  <p>I have a <a href="https://www.blogger.com/">Blogger</a> site at <a href="https://photos.jws.app/">photos.jws.app</a> where I post random photos from my day.  The goal of this pen is to get an array of the photos from the site (using <a href="https://vuejs.org/">Vue.js</a> and <a href="https://github.com/axios/axios">Axios</a>) and use the images as the background of this webpage.</p>
  
  <p>The images currently on my photo blog are displayed below.  Try clicking one of the images and see what happens to the background.</p>
  
  <p>Notice how this black box is <a href="https://www.w3schools.com/cssref/css3_pr_opacity.asp">pretty transparent</a>.  Try <a href="https://www.w3schools.com/cssref/sel_hover.asp">hovering over it</a>.</p>
  
  <div id="app">
    <span v-for="(image, index) in images" class="thumbSpan">
      <img v-bind:src="image" class="thumb" v-on:click="currentImage = index">
    </span>
  </div>
  
</div>
              
            
!

CSS

              
                body{
  background-color:black;
  background-repeat: no-repeat;
  background-position: 0 0;
  background-size: cover;
}

#description{
  background-color:black;
  color:white;
  padding:1em;
  margin-right:10em;
  margin-left:10em;
  border-radius: 10px;
  opacity: 0.8;
}

#description:hover {
  opacity: 0.9;
}

#description a{
  color:green;
}

#app{
  margin-top:3em;
  margin-bottom:3em;
}

.thumb{
  width:10em;
  margin-left:5px;
  margin-right:5px;
}
              
            
!

JS

              
                new Vue({
  el: "#app",
  data() {
    return {
      posts: null,
      images: [],
      arraySize: 0,
      currentImage: 0
    };
  },
  watch: {
    // Watch for the value of posts to change
    'posts': function() {
      // Track the array size for UI purposes
      this.arraySize = this.posts.length;
      // Once posts gets updated, this gets executed 
      for (var i = 0; i < this.arraySize; i++) {
        var x = this.posts[i].content.indexOf("href=\"");
        var y = this.posts[i].content.indexOf("\"",x+6);
        this.images.push(this.posts[i].content.substring(x+6,y));
      }
      document.body.style.backgroundImage = "url('"+this.images[this.currentImage]+"')";
    },
    // Watch for the value of currentImage to change
    'currentImage': function() {
      document.body.style.backgroundImage = "url('"+this.images[this.currentImage]+"')";
    }
  },
  mounted() {
    // Get the posts from the blogger site
    // this requires an API key
    axios
    .get("https://www.googleapis.com/blogger/v3/blogs/3412726727067752443/posts?key=AIzaSyDMDCj74G_V9RzYRUXvQLsK2AWcob7FsKw")
      .then(response=> (this.posts = response.data.items))
  }
});
              
            
!
999px

Console