react 实现点击放大效果

(一)简单的点击放大demo1

import React, { Component } from 'react';
import './App.css'
import img2 from './2.jpeg'
import Zmage from 'react-zmage'
class App extends Component {
	render() {
		return (
			<div >
				<Zmage src={img2}/>
			</div>
		);
	}
}

export default App;

(二)点击缩略图,放大图片,大图以轮播的形式展现

App.js

import React, { Component } from 'react';
import './App.css'
import { Icon } from 'semantic-ui-react'
import "semantic-ui-css/semantic.min.css";
import Swiper from 'react-id-swiper';
import './Swiper.css'
import Modal from 'react-modal';
import img2 from './2.jpeg'
import img3 from './3.jpeg'
Modal.setAppElement('div');
class App extends Component {
	constructor(props) {
		super(props);
		this.state = {
			modalIsOpen: false,
			modalImgs: [],
			direction: ''
		};
		this.openModal = this.openModal.bind(this);
		this.closeModal = this.closeModal.bind(this);
		this.Open = this.Open.bind(this);
		this.close = this.close.bind(this);
	}
	openModal() {
		this.setState({ modalIsOpen: true });
	}

	closeModal() {
		this.setState({ modalIsOpen: false });
	}
	Open(dire) {
		this.setState({ modalIsOpen: true, direction: dire })
	}
	close() {
		this.setState({ modalIsOpen: false })
	}
	render() {
		const modalflag = this.state.direction === 'left' ? true : false;
		const modalnumber = (modalflag === false ? 1 : 0)
		const params = {
			rtl: true,
			initialSlide: modalnumber,//设置默认页面
			pagination: {
				el: '.swiper-pagination',
				clickable: true,
			},
			navigation: {
				nextEl: '.swiper-button-next',
				prevEl: '.swiper-button-prev'
			}
		};
		return (
			<div className="timeContainer">
				<Modal isOpen={this.state.modalIsOpen} onRequestClose={this.closeModal} className="modalstyle">
					<Swiper {...params}>
						<div className="swipertotal">
							<img src={img2} alt="" className="bigImg"></img>
							<div className="inner">
								<p>foot:left</p>
							</div>
							<button onClick={this.close} className="closebutton">
								<Icon name="close"></Icon>
							</button>
						</div>
						<div className="swipertotal">
							<img src={img3} alt="" className="bigImg"></img>
							<div className="inner">
								<p>foot:right</p>
							</div>
							<button onClick={this.close} className="closebutton">
								<Icon name="close"></Icon>
							</button>
						</div>
					</Swiper>
				</Modal>
				<p className="timep">2019-1-16</p>
				<div className="totalbox">
					<div className="imgbox">
						<img src={img2} alt={'no img'} className="imgstyle" onClick={() => this.Open('left')} />
						<p>foot:left</p>
					</div>
					<div className="imgbox">
						<img src={img3} alt={'no img'} className="imgstyle" onClick={() => this.Open('right')} />
						<p>foot:right</p>
					</div>
					<div className="clearboth"></div>
				</div>
			</div>
		);
	}
}
export default App

App.css

.swiper-container {
  width: 100%;
  height: auto;
}
.swiper-slide {
  width: auto;
  overflow: hidden;
  text-align: center;
  font-size: 18px;
  background: #fff;
}
.clearboth{clear: both;}
.imgbox{float: left;overflow: hidden;}
.imgstyle{width: 96%;}
.timep{text-align: left;}
.closebutton{
  appearance: none;
  border: none;
  background: #FFD800;
  border-radius: 4px;
  text-transform: uppercase;
  box-sizing: border-box;
  padding: 4px 8px;
  font-weight: 400;
  font-size: 13px;
  cursor: pointer;
  position: absolute;
  right: 0;
  top: 0;
}
.closebutton:hover {
  color: #FFFFFF;
  background: #222222;
}
.swipertotal{position: relative;}
.bigImg{width: 40%;height: auto;}

Swiper.css

本人已经在另外一篇博客粘贴了Swiper.css的代码:https://blog.csdn.net/qq_37815596/article/details/86293569

实现echarts的点击放大功能,可以使用echarts提供的事件处理方法。具体实现步骤如下: 1. 在react组件中引入echarts库,并初始化一个echarts实例: ```jsx import React, { useEffect, useRef } from 'react'; import echarts from 'echarts'; function Chart() { const chartRef = useRef(null); useEffect(() => { const chart = echarts.init(chartRef.current); // 在这里配置echarts图表的数据和样式 chart.setOption({...}); }, []); return <div ref={chartRef} style={{ width: '100%', height: '500px' }}></div>; } ``` 2. 给echarts实例绑定click事件处理方法,用于放大缩小: ```jsx useEffect(() => { const chart = echarts.init(chartRef.current); // 在这里配置echarts图表的数据和样式 chart.setOption({...}); chart.on('click', (params) => { const zoom = chart.getOption().dataZoom[0]; const { startValue, endValue } = zoom; const zoomFactor = 0.1; // 缩放因子 if (params.componentType === 'dataZoom') { // 点击在dataZoom组件上,放大 const zoomStart = startValue + (endValue - startValue) * zoomFactor; const zoomEnd = endValue - (endValue - startValue) * zoomFactor; chart.dispatchAction({ type: 'dataZoom', startValue: zoomStart, endValue: zoomEnd, }); } else { // 点击在其他区域,缩小 const zoomStart = startValue - (endValue - startValue) * zoomFactor; const zoomEnd = endValue + (endValue - startValue) * zoomFactor; chart.dispatchAction({ type: 'dataZoom', startValue: Math.max(zoomStart, 0), endValue: Math.min(zoomEnd, 100), }); } }); }, []); ``` 3. 在echarts图表中添加dataZoom组件: ```jsx useEffect(() => { const chart = echarts.init(chartRef.current); // 在这里配置echarts图表的数据和样式 chart.setOption({ ..., dataZoom: [ { type: 'inside', start: 0, end: 100, }, ], }); ... }, []); ``` 这样就可以在echarts图表中实现点击缩放的功能了。点击在dataZoom组件上时放大点击在其他区域时缩小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值