About
B-Design 是阿里云推出的一套面向企业应用领域的设计系统。在企业级软件服务逐渐走向云端化的趋势下,为阿里内部及合作伙伴的 SaaS 系统上云提供标准化的设计规范和指导。@b-design/scroll 是 B-Design System 的页面级组件,基于 Alibaba Cloud Design 自研原生 JavaScript 能力,提供自动化(CSS 样式)与定制化(JS 事件)的页面滚动视效管理,及丰富的 React 使用示例。可利用 @b-design/scroll 提供的锚点样式挂钩、函数接口、滚动比例(Ratio),设计与开发丰富的滚动视效。
Live Example
Install
npm install @b-design/scroll --save
Usage
在 Javascript 中使用 @b-design/scroll
HTML
<div anchor-scroll="anchor"></div>
<div anchor-scroll="anchor" anchor-trigger="repeat" class="your-style"></div>
<div anchor-scroll="anchor" anchor-trigger="repeat" anchor-name="your-name"></div>
Javascript
import { drone } from '@b-design/scroll';
drone.update();
drone.hook(domElement, 'update', ({ name, isVisible, ratio }) => {
if (isVisible && name === 'your-name') {
}
});
drone.clear();
在 React 中使用 @b-design/scroll
import { drone } from '@b-design/scroll';
const MyComponent = (props) => {
const elRef = React.useRef(null);
React.useEffect(() => {
drone.update();
drone.hook(elRef.current, 'update', ({ name, isVisible, ratio }) => {
if (isVisible && name === 'your-name') {
elRef.current.style.transform = `translateX(${ratio * 1000}px)`;
}
});
return () => {
drone.update();
};
}, []);
return;
<div ref={elRef} anchor-scroll='anchor' anchor-name='your-name'></div>;
};
在 Vue3 中使用 @b-design/scroll
<template>
<div class="page-home page">
<h2 class="your-style"
anchor-scroll="anchor"
anchor-trigger="repeat"
anchor-name="your-name"
ref="elRef">
{{ data.title }}
</h2>
</div>
</template>
.your-style {
opacity: 0;
transition: opacity 0.4s;
&[anchor-on] {
opacity: 1;
}
}
import { ref, reactive, onBeforeUnmount, onMounted } from 'vue';
import { drone } from '@b-design/scroll';
export default {
setup(props, context) {
const elRef = ref(null);
const data = reactive({
title: 'Hey'
});
onMounted(() => {
drone.update();
drone.hook(elRef.value, 'update', ({ domElement, name, isVisible, ratio }) => {
if (isVisible && name == 'your-name') {
domElement.style.transform = `scale(${ratio})`;
}
});
});
onBeforeUnmount(() => {
drone.update();
});
return {
elRef,
data
};
}
};
在 React 中引用封装好滚动效果的 @b-design/scroll 页面级组件
import { drone } from '@b-design/scroll';
import { HorizontalMovement } from '@b-design/scroll/react';
import '@b-design/scroll/react/horizontal-movement/style.css';
const MyComponent = (props) => {
React.useEffect(() => {
drone.update();
return () => {
drone.update();
};
}, []);
return <HorizontalMovement {...someConfigs} />;
};
API Reference
DOM 元素属性
anchor-scroll="anchor"
必须。带有此属性值的元素才会被指定锚点,用于 @b-design/scroll 查找、更新、计算
anchor-trigger="repeat|once"
非必须。注意 anchor-trigger 属性仅与 CSS 样式挂钩有关,与 hook 函数事件无关
当 anchor 元素进入页面时会被自动 trigger 样式,元素会增加 anchor-on attribute 属性,
用户可以在 CSS 中通过 [anchor-on] 选择器,自定义 trigger 后的样式
"once" 元素进入页面时,仅触发一次样式挂钩
"repeat" 元素进入页面时触发,离开页面时移除 anchor-on 属性,可反复触发样式挂钩
挂钩元素样式示例
.your-style {
opacity: 0;
transition: opacity 0.4s;
}
.your-style[anchor-on] {
opacity: 1;
}
anchor-name="[your-name]"
非必须。其作用等同于标记一个字符串 ID,在 drone.hook 回调中会返回 name,用于更加定制化的事件和效果
Drone
drone
@b-design/scroll 的锚点控制与管理实例。用户侧无需创建,仅引用:
import { drone } from '@b-design/scroll';
drone.scrollTo({position, duration, ease})
页面滚动到 pageYOffset
drone.scrollToRatio({domElement, ratio, duration, ease})
页面滚动到相对于 domElement 元素在页面中的一定比例
drone.hook(domElement, type, cb)
挂钩 domElement 的 JS 函数事件,用于需要做 CSS 样式挂钩无法达到的进阶效果
注:@b-design/scroll 的事件机制与 CSS 样式的 anchor-trigger 为分离设计,
anchor-trigger 样式挂钩与 JS 事件的挂钩没有直接关系
drone.unhook(domElement, item, cb)
移除 domElement 的挂钩回调事件
原则上如无需要不必调用此接口,JS 事件机制默认为自动回收清除与管理
drone.update()
查找并更新所有的锚点
drone.clear()
清除所有锚点, 不查找更新
drone.getPosition()
返回页面 Y Offset 值
drone.updateConfig(opts)
用于首次全局常规配置更新
opts: {
triggerName?: string;
queryName?: string;
threshold?: number;
listener?: boolean;
}
Build Process
npm i
to install packagesnpm start
to start developmentnpm run lib
to get @b-design/scroll library at 'esm' foldernpm run live
to build