javascript 前端 为自己的项目媒体资源添加固定高度 - `${(ratio[1] / ratio[0]) * 100}%` : &#39

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多助力神器 👁️ 121 次阅读

)}

);

};

export default LazyLoadImg;

// css

.cover {

width: 100%;

height: 100%;

background-size: cover;

background-position: center;

background-repeat: no-repeat;

}

.lazyimg {

position: relative;

font-size: 0;

line-height: 0;

overflow: hidden;

.ratioSpan {

position: unset !important;

display: inline-block;

box-sizing: border-box;

width: initial;

height: initial;

background: none;

opacity: 0;

border: 0px !important;

margin: 0px !important;

& + img {

border-radius: inherit;

position: absolute;

inset: 0px;

box-sizing: border-box;

padding: 0px;

border: none;

margin: auto;

display: block;

width: 0px;

height: 0px;

min-width: 100%;

max-width: 100%;

min-height: 100%;

max-height: 100%;

}

}

}

视频固定高度组件

视频的懒加载函数既是将data-src的内容放到src里,这个函数在其他文章《如何优化一个很多视频的网页》中给出了。 video的固定高度放在了lazyload组件上,所以高度会一直存在,基本不存在图片固定高度组件中 滑动一定距离才加载出内部元素 & 高度的情况。

import React from 'react';

import LazyLoad from 'react-lazyload';

import styles from './index.module.less';

/**

* 存在两种懒加载方案、可以自动设置高度的video组件

* @param {Object} style video元素的style

* @param {Object} outerStyle video元素父级组件的style

* @param src 采用LazyLoad 懒加载组件加载

* @param poster 采用LazyLoad 懒加载组件加载

* @param data_src 不采用懒加载组件(否则获取不到video元素),配合VideoLazyLoad使用

* @param data_poster 不采用懒加载组件,配合VideoLazyLoad使用

* @param offset

* @param {Array} ratio 视频宽高比例,设置后需要设定父级className的宽度

* @param className

* @param autoPlay

* @param onClick

* @param {object} videoProps

* @returns

*/

const LazyLoadVideo = ({ children, ...props }) => {

let {

src,

poster,

data_src,

data_poster,

offset = 400,

ratio,

className,

autoPlay = true,

onClick,

videoProps,

outerStyle = {},

style = { width: '100%' },

key,

} = props;

return data_src ? (

) : (

key={key}

offset={offset}

className={[styles.lazyVideo, className].join(' ')}

style={{ ...outerStyle, paddingBottom: Array.isArray(ratio) ? `${(ratio[1] / ratio[0]) * 100}%` : 'auto' }}

>

src={src}

style={style}

poster={poster}

disablePictureInPicture

autoPlay={autoPlay}

loop

muted

playsInline

controls={false}

onClick={onClick}

{...videoProps}

/>

);

};

export default LazyLoadVideo;

// css

.lazyVideo {

position: relative;

width: 100%;

height: 0;

overflow: hidden;

display: inline-block;

box-sizing: border-box;

background: none;

border: 0px;

margin: 0px;

font-size: 0;

line-height: 0;

video {

object-fit: fill;

position: absolute;

inset: 0px;

box-sizing: border-box;

padding: 0px;

border: none;

margin: 0 auto;

display: block;

width: 100%;

height: 100%;

font-size: 0;

line-height: 0;

border-radius: inherit;

}

}

.lazyVideo1 {

position: relative;

font-size: 0;

line-height: 0;

.ratioSpan {

width: 100%;

inset: 0px;

height: 0;

overflow: hidden;

display: inline-block;

box-sizing: border-box;

background: none;

border: 0px;

margin: 0px;

font-size: 0;

line-height: 0;

}

video {

object-fit: fill;

position: absolute;

inset: 0px;

box-sizing: border-box;

padding: 0px;

border: none;

margin: 0 auto;

display: block;

width: 100%;

height: 100%;

border-radius: inherit;

}

}

中途查询修改目标高度的函数

其实JQUERY似乎是有一个$().animation()函数已经直接实现了这个功能,但是因为我的项目没有用到,所以就只能自己实现一下了。

const targetRef = useRef(null);

const menuHeader = useRef(null);

const timer = useRef(null);

const max_call = 10;

let max_call_count = 0;

const [res, setRes] = useState([]);

const scrollToElement = async (lastScrollTop, lastWindowScrollY, lastElementTop) => {

// 安卓手机端兼容性

const windowScrollY = document.documentElement.scrollTop || document.body.scrollTop;

const nowElementTop = targetRef.current.offsetTop;

clearTimeout(timer.current);

timer.current = null;

const margin = isMobile ? (menuHeader.current ? menuHeader.current.clientHeight : 80) : 50;

const target = nowElementTop - margin;

const step1 = nowElementTop / 3;

const step2 = nowElementTop * 0.75;

let scrollTop = 0; // 记录本次指定的高度,避免反复赋值造成卡顿

// 分阶段赋予高度,避免过快跳转导致中间有高度变化查询不到

if (windowScrollY < step1 - 300) {

scrollTop = step1;

}

if (windowScrollY >= step1 - 300 && windowScrollY < step2 - 300) {

scrollTop = step2;

}

if (windowScrollY >= step2 - 300) {

scrollTop = target;

}

if (isReachBottom(10)) {

max_call_count = 0;

return window.scrollTo(0, targetRef.current.offsetTop);

}

if (targetRef.current.getBoundingClientRect().top.toFixed(0) > margin) {

// 判断当前高度 避免反复赋值造成卡顿

if (windowScrollY >= lastScrollTop - 300 || nowElementTop.toFixed(0) !== lastElementTop.toFixed(0)) {

window.scrollTo({

top: scrollTop,

left: 0, 拼多多帮助力入口

behavior: 'instant',

});

}

// 每100ms更新一次

if (max_call_count < max_call) {

if (lastWindowScrollY === windowScrollY) max_call_count += 1;

timer.current = setTimeout(() => {

scrollToElement(scrollTop, windowScrollY, nowElementTop);

}, 90);

}

}

};

// 避免懒加载一直加载导致屏幕滚动失败,将路程分为4段,每隔一段查询一次目标高度,直到滚动到目标位置

const handleScrollToTarget = async () => {

try {

// 查询目标

let firstScrollTop = 0;

if (!targetRef.current) {

const t = await querySelector('#target').then((ele) => {

targetRef.current = ele;

return ele;

});

if (!t) return;

firstScrollTop = t.offsetTop;

} else {

firstScrollTop = targetRef.current.offsetTop;

}

// 查询documentElement & 错误捕获

const documentElement =

document.documentElement ||

(await new Promise((resolve, reject) => {

const interval = setInterval(() => {

if (max_call_count < max_call) {

if (!!document.documentElement) {

max_call_count = 0;

clearInterval(interval);

resolve(document.documentElement);

} else {

max_call_count++;

}

} else {

clearInterval(interval);

max_call_count = 0;

resolve(null);

}

}, 100);

}));

if (documentElement || document.documentElement) {

window.scrollTo(0, firstScrollTop);

scrollToElement(firstScrollTop / 2, 0, firstScrollTop);

} else {

throw Error('无法获取document.documentElement');

}

} catch (e) {

console.error('滚动函数失败——', e);

// 兜底函数,直接滚动

}

};

/**

* 页面触底判断

* @param margin 触底函数判断范围

*/

function isReachBottom(margin = 36) {

// 窗口高度

var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;

// 滚动高度

var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

// 页面高度

var documentHeight = document.documentElement.scrollHeight || document.body.scrollHeight;

if (windowHeight + scrollTop + margin > documentHeight) {

return true;

} else {

return false;

}

}

相关链接

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧