如何修复水平滚动时图像之间间距过大。

huangapple go评论62阅读模式
英文:

How to fix too much space between images when using horizontal scroll

问题

I'm trying to build a basic movie app using the OMDB api with a basic interface and search functionality implemented so far. When the results show up, there's a huge space between each movie poster. I'm still learning and cannot figure out how to fix this.

The page

.movie-app > .row{
  overflow-x: auto;
  flex-wrap: nowrap;
}

这是我用来实现水平滚动的 CSS 代码。

const App = () => {
  const [movies, setMovies] = useState([]);
  const [searchValue, setSearchValue] = useState("");

  const getMovieRequest = async () => {
    const url = `http://www.omdbapi.com/?s=${searchValue}&apikey=473e63b8`;

    const response = await fetch(url);
    const responseJson = await response.json();

    if (responseJson.Search) {
      setMovies(responseJson.Search);
    }
  };

  useEffect(() => {
    getMovieRequest(searchValue);
  }, [searchValue]);

  return (
    <div className="container-fluid movie-app">
      <div className="row d-flex align-items-center mt-4 mb-4">
        <MovieListHeading heading="Movies" />
        <SearchBox searchValue={searchValue} setSearchValue={setSearchValue} />
      </div>
      <div className="row">
        <MovieList movies={movies} />
      </div>
    </div>
  );
};

export default App;

App.js

const MovieList = (props) => {
  return (
    <>
      {props.movies.map((movie, index) => (
        <div className="image-container d-flex justify-content-start m-3" key={index}>
          <img src={movie.Poster} alt="movie"></img>
        </div>
      ))}
    </>
  );
};

export default MovieList;

电影列表组件

英文:

I'm trying to build a basic movie app using the OMDB api with a basic interface and search functionality implemented so far. When the results show up, there's a huge space between each movie poster. I'm still learning and cannot figure out how to fix this.

The page

.movie-app &gt; .row{
  overflow-x: auto;
  flex-wrap: nowrap;
}

This is the css code I have used to implement the horizontal scroll.

const App = () =&gt; {
  const [movies, setMovies] = useState([]);
  const [searchValue, setSearchValue] = useState(&quot;&quot;);

  const getMovieRequest = async () =&gt; {
    const url = `http://www.omdbapi.com/?s=${searchValue}&amp;apikey=473e63b8`;

    const response = await fetch(url);
    const responseJson = await response.json();

    if (responseJson.Search) {
      setMovies(responseJson.Search);
    }
  };

  useEffect(() =&gt; {
    getMovieRequest(searchValue);
  }, [searchValue]);

  return (
    &lt;div className=&quot;container-fluid movie-app&quot;&gt;
      &lt;div className=&quot;row d-flex align-items-center mt-4 mb-4&quot;&gt;
        &lt;MovieListHeading heading=&quot;Movies&quot; /&gt;
        &lt;SearchBox searchValue={searchValue} setSearchValue={setSearchValue} /&gt;
      &lt;/div&gt;
      &lt;div className=&quot;row&quot;&gt;
        &lt;MovieList movies={movies} /&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
};

export default App;

App.js

const MovieList = (props) =&gt; {
  return (
    &lt;&gt;
      {props.movies.map((movie, index) =&gt; (
        &lt;div className=&quot;image-container d-flex justify-content-start m-3&quot;&gt;
          &lt;img src={movie.Poster} alt=&quot;movie&quot;&gt;&lt;/img&gt;
        &lt;/div&gt;
      ))}
    &lt;/&gt;
  );
};

export default MovieList;

Movie List component

答案1

得分: 0

根据文档,Bootstrap中的row类默认使节点内的列等宽。要使列适应节点内容的宽度,请尝试使用row row-cols-auto

不要这样做:

<div className="row">
  <MovieList movies={movies} />
</div>

而是尝试这样做:

<div className="row row-cols-auto">
  <MovieList movies={movies} />
</div>
英文:

According to document, the row class in bootstrap makes the columns inside the node equal-width as default. To make the columns be fit by content width of the node, try to use row row-cols-auto.

Instead of this:

  &lt;div className=&quot;row&quot;&gt;
    &lt;MovieList movies={movies} /&gt;
  &lt;/div&gt;

Try this instead:

  &lt;div className=&quot;row row-cols-auto&quot;&gt;
    &lt;MovieList movies={movies} /&gt;
  &lt;/div&gt;

huangapple
  • 本文由 发表于 2023年6月29日 14:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578584.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定