The image is not displaying on the page

Slider Section (Gray Part) We verified after loading, and the data was spot on. In development mode (F12), we can inspect object information (ImgURL, Text, etc.). However, it is not being displayed.

Below is the template code and script code. Thank you.

<template>
    <div class="home-slider-container" v-if="hasResult">
        <div class="home-slider owl-carousel">
            <div class="home-slide" v-for="(banner, index) in banners">
                <div class="slide-bg owl-lazy" v-bind:data-src=banner.imgUrl></div><!-- End .slide-bg -->
                <div class="home-slide-content container">
                    <div class="row">
                        <div class="col-md-6 offset-md-6 col-lg-5 offset-lg-7">
                            <h4>{{banner.topText}}</h4>
                            <h1>{{banner.middleText}}</h1>
                            <h3><strong>{{banner.bottomText}}</strong></h3>
                            <a href="category.html" class="btn btn-primary">Go Now</a>
                        </div><!-- End .col-lg-5 -->
                    </div><!-- End .row -->
                </div><!-- End .home-slide-content -->
            </div><!-- End .home-slide -->
        </div><!-- End .home-slider -->
    </div><!-- End .home-slider-container -->
</template>

<script>
    export default {
        name: 'MainHomeSlider',
        data: function () {
            return {
                banners: []
            }
        },
        computed: {
            hasResult: function () {
                return this.banners.length > 0
            }
        },
        created() {
            const baseURI = 'Server API address';
            this.$http.get(`${baseURI}/v1.0/banners`)
                .then((result) => {
                    console.log(result.data.result.banners)
                    console.log(typeof (result.data.result.banners))
                    this.banners = result.data.result.banners
                })
        }
    }
</script>

CSS Styles for OWL Carousel:

.owl-carousel {
  /* CSS styles */
}
...
/* Other CSS styles here */
...
.owl-carousel.owl-rtl .owl-item {
  float: right;
}

You can request the API from

Data Structure: I confirmed that the Img URL should be accessible. {"result": {"banners": [{"topText":"Banner test","middleText":"Banner test","bottomText":"Banner test","imgUrl":"ImgURL"}, {"topText":"Banner test","middleText":"Banner test","bottomText":"Banner test","imgUrl":"ImgURL"}]} ,"message":"OK","status":200}

Answer №1

I didn't see where you were implementing the use of data-src in your CSS to set the div's background-image. Using the css function attr won't work for this purpose.

Instead, I suggest using a :style binding to set the image URL. You can reference an example on this codepen.

const initData = {"result":{"banners":[{"topText":"Banner test","middleText":"Banner test","bottomText":"Banner test","imgUrl":"https://s3.ap-northeast-2.amazonaws.com/baekdoo/test/1b848efc76a047a5970a04712ca51c46.jpg"},{"topText":"Banner test","middleText":"Banner test","bottomText":"Banner test","imgUrl":"https://s3.ap-northeast-2.amazonaws.com/baekdoo/banner1/5eabca00f6394ee99a5cf8191b1b018f.jpg"}]},"message":"OK","status":200}

new Vue({
el: "#app",
data: function () {
return {
banners: []
}
},
computed: {
hasResult: function () {
return this.banners.length > 0
}
},
methods: {
imgBG: img => {
return ({'background-image': `url('${img}')` })
}
},
created() {
// insert your API code here
this.banners = initData.result.banners
}
});
.slide-bg {
width: 100px;
height: 100px;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
.owl-carousel {
  // display: none;
  width: 100%;
  -webkit-tap-highlight-color: transparent;
  /* position relative and z-index fix webkit rendering fonts issue */
  position: relative;
  z-index: 1;
}
.owl-carousel .owl-stage {
...
...
</script>
<div id="app">
   <div class="home-slider-container" v-if="hasResult">
        <div class="home-slider owl-carousel">
            <div class="home-slide" v-for="(banner, index) in banners">
                <div class="slide-bg owl-lazy" :style=imgBG(banner.imgUrl)></div><!-- End .slide-bg -->
                <div class="home-slide-content container">
                    <div class="row">
                        <div class="col-md-6 offset-md-6 col-lg-5 offset-lg-7">
                            <h4>{{banner.topText}}</h4>
                            <h1>{{banner.middleText}}</h1>
                            <h3><strong>{{banner.bottomText}}</strong></h3>
                            <a href="category.html" class="btn btn-primary">Go to Category</a>
                        </div><!-- End .col-lg-5 -->
                    </div><!-- End .row -->
                </div><!-- End .home-slide-content -->
            </div><!-- End .home-slide -->
        </div><!-- End .home-slider -->
    </div><!-- End .home-slider-container -->
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is preventing Web API from triggering a CORS error in browsers such as Chrome and Edge, as well as the Postman tool?

While working on developing an API in Asp.Net Core 3.1, everything seemed to be functioning properly. However, I encountered CORS-related errors when attempting to send requests via ajax. Interestingly, these errors were not present when sending GET reques ...

Retrieve items from a JSON file using Ionic framework's $http.get method

Issue with Console: SyntaxError: Unexpected token { in JSON at position 119 Xcode controller: str="http://www.website.com/user-orders.php?e="+$scope.useremail; $http.get(str) .success(function (response){ $scope.user_orders = response; ses ...

There was a failure to establish a Redis connection to the server with the address 127.0.0.1 on port 6379

Currently, I am working with node.js using expressjs. My goal is to store an account in the session. To test this out, I decided to experiment with sessions by following the code provided on expressjs var RedisStore = require('connect-redis')(ex ...

Modifying the content inside a div element and inserting an image into it

I am facing a problem where I am unable to display the image when trying to change the inner HTML of a div by inserting an img source into it. Below is my JavaScript code snippet: var sliderimg = document.getElementById('abc_'+obj).src; $("#eve ...

Modify a JavaScript object in JSON format using another object as reference

Consider two JSON formatted JavaScript objects: obj1 = { prop1: 1, prop2: 2, prop3: 3 } obj2 = { prop1: 1, prop2: 3 } In the context of jQuery or Angular, what is the recommended practice to update obj2 into obj1 while also re ...

What is the best way to retrieve data from an Express endpoint within a React component?

I am working on integrating a React component with an Express endpoint that returns the string "sample data". The goal is to call this endpoint from my React app, store the text in state, and then display it on the screen. Here is my component: class App ...

Combining meshes results in a lower frame rate

I have combined approximately 2500 meshes, each with its own color set, but my FPS is lower than if I had not merged them. Based on THIS article, merging is recommended to improve FPS performance. Is there something I am overlooking? var materials = new ...

How does a browser decide to load content from an http URL when the frontend source is using an https URL?

When using my Vue component to load external content in an iframe, everything works fine locally. However, once I deploy it to my HTTPS site, I encounter an issue. <iframe src="https://external-site" /> Upon deployment, I receive the following erro ...

Rotating an image as it approaches the footer: a step-by-step guide

I'm trying to add a navigation arrow on my website that will rotate to point to the top when it reaches the footer. When clicked on, it should scroll back up to the top. The arrow already has a scrolling effect, but I need help figuring out how to m ...

Set the error state of a TextField in Material UI to true based on the user's input

Being a newcomer to React and Javascript, I have made some progress but now find myself stuck. I am struggling with how to change the error state of a Material UI TextField based on user input. Specifically, I want the error to be triggered if the user inp ...

ensure that angular's ng-if directive sets aside space to display content when triggered by an event

I'm facing an issue with my modal form setup where if a text-field is left blank, a label saying "it is required" is generated below the field, causing the "Proceed" button to move (along with all other elements below it). How can I make sure that DOM ...

Why do my paths encounter issues when I alter the manner in which I deliver my index.html file?

I recently made a decision to change the method of serving my index.html file from app.use('/', express.static(__dirname + '/..')); to app.get('/', function(req, res) { res.sendFile(path.join(__dirname + '/../index.htm ...

Utilizing Radio buttons for validation in react-hook-form

After creating a form with radio buttons and implementing validation using React Hook Form, I encountered an issue where the console always displayed "on" regardless of the selected radio button. <div className=""> <label ...

Utilizing Laravel for Making Ajax Calls to Controller

I am a beginner in using Laravel and I am trying to create an API with Laravel using AJAX calls. However, when I make the AJAX call, the URL appears to show an invalid server path. Below is my code: My Route file : Route::get("a/b","AController@c"); My ...

Assign object properties to a constant variable while validating the values

When receiving the features object, I am assigning its values to constants based on their properties. const { featureCode, featureSubType, contentId, price, family: { relationCountsConfig: { motherCount, fatherCount, childrenCount }, max ...

Utilize toggle functionality for page rotation with rxjs in Angular framework

Managing a project that involves a container holding multiple cards across different pages can be overwhelming. To address this, the screen automatically rotates to the next page after a set time interval or when the user presses the space bar. To enhance ...

Issue with bouncing dropdown menu

I am in the process of enhancing a Wordpress website by implementing a jQuery menu with sub-menus. Below is the jQuery code snippet: $(document).ready(function(){ $('.menu > li').hover(function(){ var position = $(this).position(); ...

Is there a way to create an effect where the color of a floating action button changes when you hover over it, similar to a filter?

I'm new to using the <Fab /> element and I'm struggling to figure out how to implement a hover effect that will change the button's color. Here's what I have so far: JavaScript: <Fab variant="extended" className=&quo ...

Personalizing navigation tabs using Bootstrap

I've been working on creating a custom bootstrap navbar tabs, but I'm struggling to remove the borders and apply the custom colors. After some trial and error, here's what I have so far: .nav-tabs, .nav-pills { text-align: center; bo ...

A guide on switching out an HTML element with an AJAX response

How can I dynamically replace an HTML element with Ajax response? I know how to remove the element, but I'm unsure how to then insert the new content from the Ajax call. For instance, let's say I have the following code: <ul id="products"> ...