的解决方案如下:
import React, { useRef } from 'react';
const MyComponent = () => {
const scrollRef = useRef(null);
// 其他组件代码
return (
<div>
<div ref={scrollRef}>
{/* 要滚动的内容 */}
</div>
<iframe src="..." />
</div>
);
};
export default MyComponent;
import React, { useRef, useEffect } from 'react';
import ReactDOM from 'react-dom';
const MyComponent = () => {
const scrollRef = useRef(null);
useEffect(() => {
const iframe = ReactDOM.findDOMNode(scrollRef.current).nextSibling;
iframe.parentNode.style.overflow = 'hidden';
}, []);
// 其他组件代码
return (
<div>
<div ref={scrollRef}>
{/* 要滚动的内容 */}
</div>
<iframe src="..." />
</div>
);
};
export default MyComponent;
import React, { useRef, useEffect } from 'react';
import ReactDOM from 'react-dom';
const MyComponent = () => {
const scrollRef = useRef(null);
useEffect(() => {
const iframe = ReactDOM.findDOMNode(scrollRef.current).nextSibling;
iframe.parentNode.style.overflow = 'hidden';
const scrollElement = ReactDOM.findDOMNode(scrollRef.current);
scrollElement.scrollIntoView({ behavior: 'smooth', block: 'end', inline: 'nearest' });
}, []);
// 其他组件代码
return (
<div>
<div ref={scrollRef}>
{/* 要滚动的内容 */}
</div>
<iframe src="..." />
</div>
);
};
export default MyComponent;
这样,当组件加载完成后,滚动元素将会滚动到底部,而iFrame的父元素将不会滚动。请注意,这个解决方案假设iFrame和滚动元素是兄弟元素,如果它们的关系不同,请相应地调整代码。
领取专属 10元无门槛券
手把手带您无忧上云