简体   繁体   English

更改图片onMouseover | ReactJs

[英]Change Image onMouseover | ReactJs

I have an Card Component. 我有一个卡组件。 It contains an image and text. 它包含图像和文本。 By default, the image will be an redImage and the text are black. 默认情况下,图像为redImage,文本为黑色。 Onmouseover on that card, the default redimage should change to whiteimage and the text need to change into white color. Onmouseover在该卡上,默认的redimage应更改为whiteimage,文本需要更改为白色。 I am displaying the card contents using map method. 我正在使用map方法显示卡片内容。 Now, i can change the color, while mouseover using css, but, i can't change the image properly. 现在,我可以改变颜色,同时使用css鼠标悬停,但是,我无法正确更改图像。 But, i can change the image on hover, if i am not using the map function by hardcoding the content directly in the component. 但是,我可以在悬停时更改图像,如果我没有通过直接在组件中硬编码内容来使用地图功能。 After using map method only, i am facing the issue. 仅使用map方法后,我正面临着这个问题。 Please let me know, how can I achieve that. 请让我知道,我怎样才能做到这一点。 Please find my code below. 请在下面找到我的代码。

/***App***/

import React,{ Component } from 'react';
import SchDic from './SchDic'

class App extends Component {
constructor(props){
super(props);

this.state ={
Lists : [
{id:1, imgRed:require('../assets/images/red/das-red.svg'), imgWhite: require('../assets/images/white/das-whi.svg'),
Name: 'All-in-1 Dashboard',
Details:'Easily navigate the simple-to-use dashboard to track your volunteers, manage your opportunities and relationships, and gain access to advanced reporting.'},
{id:2, imgRed:require('../assets/images/red/scr-red.svg'), imgWhite: require('../assets/images/white/dig-whi.svg'),
Name: 'Screening Organizations',
Details:'Control the opportunities visible to your students by screening organizations. Invite your partnered nonprofits.' },
{id:3, imgRed:require('../assets/images/red/dig-red.svg'), imgWhite: require('../assets/images/white/pos-whi.svg'),
Name: 'Digitize Submission',
Details:'View all your student submissions to see what’s pending, approved or rejected.'},
{id:4, imgRed:require('../assets/images/red/tra-red.svg'), imgWhite: require('../assets/images/white/scr-whi.svg'),
Name: 'Tracking & Reporting',
Details:'Get up-to-date reports about how students are progressing with their commitments or requirements. Gain access to customizable analytics about individuals or groups of students.'},
{id:5, imgRed:require('../assets/images/red/pos-red.svg'), imgWhite: require('../assets/images/white/sup-whi.svg'),
Name: 'Post School-Run Events',
Details:'Get administration involved by postings school-run volunteering events directly on your private Opportunity Board..'},
{id:6, imgRed:require('../assets/images/red/sup-red.svg'), imgWhite: require('../assets/images/white/tra-whi.svg'),
Name: 'Support',
Details:'Get access to tons of resources on our FAQ or contact our team directly. We take pride in our commitment in helping you build your community.'},
],
};
}
render() {
return (
<div className="App" >
<SchDic Lists = {this.state.Lists}/>
</div>
);
}
}

export default App;

/***SchDiv***/

import React,{ Component } from 'react';
import { Card,CardMedia,CardHeader,CardContent,Typography,withStyles } from '@material-ui/core';

const Styles = {
card: {
width:'385px',
height:'241px',
padding:'0px',
margin:'15px',
cursor: 'pointer',
'&:hover': {
background: '#E8583E',
color:'white',
}
},

media: {
height: 41,
maxWidth:41,
margin:'15px',
},

name:{
padding:'1px',
margin:'15px',

},
details:{
fontSize: '14px',
padding: '0 15px',
minHeight: '25px',
align: 'left',
},
};

class SchDic extends Component {
constructor(props){
super(props);
this.state = {
value: 0,
img: require('../assets/images/red/das-red.svg'),
imgOne: require('../assets/images/red/dig-red.svg'),
imgTwo: require('../assets/images/red/pos-red.svg'),
imgThree: require('../assets/images/red/scr-red.svg'),
imgFour: require('../assets/images/red/sup-red.svg'),
imgFive: require('../assets/images/red/tra-red.svg'),

};
this.handleMouseOver = this.handleMouseOver.bind(this);
this.handleMouseOut = this.handleMouseOut.bind(this);
} 
handleChange = (event, value) => {
this.setState({ value });
};

handleMouseOver(val) {
if(val === 0){
this.setState({
img: require('../assets/images/white/das-whi.svg')
});
} else if(val === 1){
this.setState({
imgOne: require('../assets/images/white/dig-whi.svg')
});
} else if(val === 2){
this.setState({
imgTwo: require('../assets/images/white/pos-whi.svg')
});
} else if(val===3){
this.setState({
imgThree: require('../assets/images/white/scr-whi.svg')
});
} else if(val===4){
this.setState({
imgFour: require('../assets/images/white/sup-whi.svg')
});
} else {
this.setState({
imgFive: require('../assets/images/white/tra-whi.svg')
});
}
}
handleMouseOut(val) {
this.setState({
img: require('../assets/images/red/das-red.svg'),
imgOne: require('../assets/images/red/dig-red.svg'),
imgTwo: require('../assets/images/red/pos-red.svg'),
imgThree: require('../assets/images/red/scr-red.svg'),
imgFour: require('../assets/images/red/sup-red.svg'),
imgFive: require('../assets/images/red/tra-red.svg'),
});
}
render(){
const { classes }= this.props
const { Lists } = this.props;
const Post = Lists.map(List => {
return(
<div >
<Card className={classes.card} onMouseOver={() => this.handleMouseOver(List.id)} onMouseOut={this.handleMouseOut} elevation={1}>
<CardMedia
className={classes.media}
image={List.imgRed}
/>
<CardHeader className={classes.name}
titleTypographyProps={{variant:'h5' }}
title={List.Name}
/>
<CardContent className={classes.details} >
<Typography variant='Body2' color=" " component="p">
{List.Details}
</Typography>
</CardContent>
</Card>
</div>
)}
);
const divStyle = {
paddingLeft:'350px',
paddingRight:'150px',
display: 'flex',
flexWrap: 'wrap',
};
return(
<div className="coreFeatures" style={divStyle} >
{ Post }
</div>
)
}
}

export default withStyles(Styles)(SchDic);

"well i also stuck in the similar problem but i got the solution which really works just create an image folder in public folder of ur react project “好吧,我也陷入了类似的问题,但我得到的解决方案真正起作用只是在你的反应项目的公共文件夹中创建一个图像文件夹

now i created a tag in one of the react component as: 现在我在其中一个反应组件中创建了一个标签:

<img src= {process.env.PUBLIC_URL + '/image/xyz.png'} />

now i want this image to change by using mouseover listener 现在我希望通过使用鼠标悬停监听器来更改此图像

<img src= {process.env.PUBLIC_URL + '/image/xyz.png'} onMouseOver={() => this.changeImg()}/>

i defined the changeImg function as: 我将changeImg函数定义为:

 changeLogo= () => { var a= document.querySelector('.logoA');
               a.setAttribute("src",'./images/logoB.svg')
              }

but the problem is ...(just read this post) 但问题是......(刚读过这篇文章)

https://facebook.github.io/create-react-app/docs/using-the-public-folder " https://facebook.github.io/create-react-app/docs/using-the-public-folder

Answer For My Question, 回答我的问题,

import React,{ Component } from 'react';
import SchDic from './SchDic'

class App extends Component {
constructor(props){
super(props);

this.state ={
Lists : [
{id:1, imgRed:require('../assets/images/red/das-red.svg'), imgWhite: require('../assets/images/white/das-whi.svg'),
Name: 'All-in-1 Dashboard',
Details:'Easily navigate the simple-to-use dashboard to track your volunteers, manage your opportunities and relationships, and gain access to advanced reporting.'},
{id:2, imgRed:require('../assets/images/red/scr-red.svg'), imgWhite: require('../assets/images/white/dig-whi.svg'),
Name: 'Screening Organizations',
Details:'Control the opportunities visible to your students by screening organizations. Invite your partnered nonprofits.' },
{id:3, imgRed:require('../assets/images/red/dig-red.svg'), imgWhite: require('../assets/images/white/pos-whi.svg'),
Name: 'Digitize Submission',
Details:'View all your student submissions to see what’s pending, approved or rejected.'},
{id:4, imgRed:require('../assets/images/red/tra-red.svg'), imgWhite: require('../assets/images/white/scr-whi.svg'),
Name: 'Tracking & Reporting',
Details:'Get up-to-date reports about how students are progressing with their commitments or requirements. Gain access to customizable analytics about individuals or groups of students.'},
{id:5, imgRed:require('../assets/images/red/pos-red.svg'), imgWhite: require('../assets/images/white/sup-whi.svg'),
Name: 'Post School-Run Events',
Details:'Get administration involved by postings school-run volunteering events directly on your private Opportunity Board..'},
{id:6, imgRed:require('../assets/images/red/sup-red.svg'), imgWhite: require('../assets/images/white/tra-whi.svg'),
Name: 'Support',
Details:'Get access to tons of resources on our FAQ or contact our team directly. We take pride in our commitment in helping you build your community.'},
],
};
}
render() {
return (
<div className="App" >
<SchDic Lists = {this.state.Lists}/>
</div>
);
}
}

export default App;

/***SchDiv***/

    import React,{ Component } from 'react';
import { Card,CardMedia,CardHeader,CardContent,Typography,withStyles } from '@material-ui/core';

const Styles = {
    card: {
        width:'385px',
        height:'241px',
        padding:'0px',
        margin:'15px',
        cursor: 'pointer',
        '&:hover': {
            background: '#E8583E',
            color:'white',
            "& $imgOne": {
                display: 'none'
            },
            "& $imgTwo": {
                display: 'block'
            },
        },
    },
    media: {
        height: 41,
        maxWidth:41,
        margin:'15px',
        "& + $imgOne": {
            display: 'block'
        },
        "& + $imgTwo": {
            display: 'none'
        }
    },
    imgOne: {},
    imgTwo: {},
    name:{
        padding:'1px',
        margin:'15px',
    },
    details:{
        fontSize: '14px',
        padding: '0 15px',
        minHeight: '25px',
        align: 'left',
    },
};

class SchDic extends Component {
constructor(props){
super(props);
this.state = {
value: 0,

};
} 
handleChange = (event, value) => {
this.setState({ value });
};

render(){
    const { classes }= this.props
    const { Lists } = this.props;
    const Post = Lists.map(List => {
    return(
        <div >
            <Card className={classes.card} elevation={1}>
                <CardMedia
                    className={`${classes.media} ${classes.imgOne}`}
                    image={List.imgRed}
                />
                <CardMedia
                    className={`${classes.media} ${classes.imgTwo}`}
                    image={List.imgWhite}
                />
                <CardHeader className={classes.name}
                    titleTypographyProps={{variant:'h5' }}
                    title={List.Name}
                />
                <CardContent className={classes.details} >
                    <Typography variant='Body2' color=" " component="p">
                    {List.Details}
                    </Typography>
                </CardContent>
            </Card>
        </div>
    )}
);
const divStyle = {
paddingLeft:'350px',
paddingRight:'150px',
display: 'flex',
flexWrap: 'wrap',
};
return(
<div className="coreFeatures" style={divStyle} >
{ Post }
</div>
)
}
}

export default withStyles(Styles)(SchDic);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM