【发布时间】:2020-06-10 10:19:43
【问题描述】:
我正在使用 Reactjs、Axios 和 MovieDB API 制作电影搜索应用程序。我目前正在使用以下代码获取 API 的背景图片:
const backgroundStyle = {
backgroundImage: `url(https://image.tmdb.org/t/p/w500${this.state.background})`,
backgroundSize: "cover",
height: "100vh"
}
我想为背景图片添加一个线性渐变,但是当我尝试这样做时:
background: linear-gradient
或
background: linearGradient
react 还是黄色的,不知道怎么实现。我要使用的代码是:
linear-gradient( rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5) ), `url(https://image.tmdb.org/t/p/w500${this.state.background})`;
但是放入 Reactjs 格式。
完整代码在这里:
import React from 'react';
import axios from 'axios';
import '../CSS/style.css'
export default class Movielist extends React.Component {
state = {
title: "",
popularity: "",
poster: "",
background: "",
}
clickHandler = (event) => {
if (event.keyCode === 13) {
const query = event.target.value;
const API_KEY = 'caf02a958f137f43327649b2b8721302';
axios.get(`https://api.themoviedb.org/3/search/movie?api_key=${API_KEY}&query=${query}`)
.then(res => {
const title = res.data['results'][0]['title'];
this.setState({ title });
const popularity = res.data['results'][0]['popularity']
this.setState({ popularity });
const poster = res.data['results'][0]['poster_path']
this.setState({ poster });
const background = res.data['results'][0]['backdrop_path']
this.setState({ background })
})
}
}
render() {
const backgroundStyle = {
backgroundImage: `url(https://image.tmdb.org/t/p/w500${this.state.background})`,
backgroundSize: "cover",
height: "100vh"
}
return (
<div id="main-div" style={backgroundStyle}>
<div id="second-div">
<input type="search" id="search" onKeyDown={event => this.clickHandler(event)} />
<h1 id="title">Title: {this.state.title}</h1>
<h1 id="popularity">Popularity: {this.state.popularity}</h1>
<img id="poster" src={`https://image.tmdb.org/t/p/w300${this.state.poster}`} />
</div>
</div>
)
}
}
【问题讨论】: