简体   繁体   English

点击按钮从 React.js function 返回图像

[英]Return image from React.js function on button click

I am new to React.js and I am trying to create SlideShow....我是 React.js 的新手,我正在尝试创建 SlideShow....

At first when page loades one image will be loaded (first one in array), then when user clicks left on tag <a> the function slides() should return path from global variable .首先,当页面加载时,将加载一个图像(数组中的第一个),然后当用户在tag <a>上单击左键时,function slides()应该从全局变量返回路径 Images are imported right and they are in array... whenever page loads only first picture is visible and it's impossible to change between images.图像被正确导入并且它们在数组中......每当页面加载时,只有第一张图片是可见的,并且不可能在图像之间进行更改。

What can I do so I can return path to <img> tag so right image would appear?我该怎么做才能返回<img>标签的路径以便出现正确的图像?

import bg1 from "../images/bg1.jpg";
import bg2 from "../images/bg2.jpg";
import bg3 from "../images/bg3.jpg";

global.images = [
    bg1, bg2, bg3
];
function slides(n) {
    return(
        global.images[n]
    );
}
<div className="slideShow">
   <img alt="Image" src={slides(0)} className="w-100 h-100"/>
   <a href="" className="prev" onClick={slides(1)}>&#10094;</a>
   <a href="" className="next" onClick={slides(2)}>&#10095;</a>
</div>

You need some way of tracking the active slide number after you can just change the image src在您可以更改图像 src 之后,您需要一些方法来跟踪活动幻灯片编号

let currentSlide = 0;

let images = ["src1", "src2", "src3"];

function changeSlide(amount) {
  currentSlide += amount;

  currentSlide = currentSlide < 0 ? images.length : currentSlide;

  currentSlide = currentSlide > images.length ? images.length : currentSlide;

  let slideShow = document.getElementById("image");

  slideShow.src = images[currentSlide];
}

changeSlide(1);

changeSlide(-1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React.js从另一个按钮调用按钮单击 - React.js calling a button click from a different button 点击提交按钮,执行 function 然后提交表单 react.js - on submit button click, execute function then submit form react.js 通过单击按钮更改功能中的状态不会触发React.js中的重新渲染 - Changing state in a function from a button click does not trigger a rerender in React.js React.js按钮未从另一个文件调用函数 - React.js button not calling function from another file React.js 图像滑块后退按钮 - React.js Image Slider Back Button 从 React.js 按钮打开下拉菜单后,防止按钮再次单击? - Prevent the button click again once the dropdown menu has opened from the button React.js? React.js:如何在将变量传递给click函数时使用相同的按钮 - React.js: How to use the same button while passing variables to a click function 单击图像图标时,请参考REACT.js中的输入类型文件功能 - On click of image icon, refer the input type file function in REACT.js 如何将参数传递给其他组件以在react.js中单击按钮时显示API列表的详细信息 - How to pass parameter to other component to show details of list from API on button click in react.js 我想从 react.js 中单击按钮的选择选项中获取选定的值 - I want to get selected value from select options on button click in react.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM