press-ui
Version:
简单、易用的跨端组件库,兼容 Vue2 和 Vue3,同时支持 uni-app和普通 Vue 项目
47 lines (40 loc) • 1.22 kB
JavaScript
import { getRect } from '../common/dom/rect';
import { requestAnimationFrame } from '../common/utils/system';
export function getScrollDurationAndWidth({
contentSelect = '.press-notice-bar__content',
wrapSelector = '.press-notice-bar__wrap',
context,
speed = 60,
scrollable = true,
}) {
return new Promise((resolve, reject) => {
requestAnimationFrame(() => {
Promise.all([
getRect(context, contentSelect),
getRect(context, wrapSelector),
])
.then((rects) => {
const [contentRect, wrapRect] = rects;
if (contentRect == null
|| wrapRect == null
|| !contentRect.width
|| !wrapRect.width
|| scrollable === false) {
reject();
return;
}
if (scrollable || wrapRect.width < contentRect.width) {
const duration = ((wrapRect.width + contentRect.width) / speed) * 1000;
resolve({
wrapWidth: wrapRect.width,
contentWidth: contentRect.width,
duration,
});
}
})
.catch((err) => {
reject(err);
});
});
});
}