Floating Button Plugin
一个轻量级的浮动按钮插件,支持自定义样式、图标和位置。默认显示在页面左下角。
环境要求
安装
使用 npm 安装:
npm install floating-button-plugin
或使用 yarn 安装:
yarn add floating-button-plugin
使用方法
基础用法
import FloatingButton from 'floating-button-plugin';
new FloatingButton({
text: '联系我们',
link: 'https://example.com/contact'
});
带图标的按钮
new FloatingButton({
text: '在线客服',
link: 'https://example.com/service',
icon: {
type: 'fontawesome',
value: 'fas fa-headset'
}
});
new FloatingButton({
text: '微信客服',
link: 'https://example.com/wechat',
icon: {
type: 'url',
value: 'https://example.com/wechat-icon.png',
width: '24px'
}
});
自定义位置
new FloatingButton({
text: '在线客服',
link: 'https://example.com/service',
initialPosition: {
x: window.innerWidth - 190 - 10,
y: window.innerHeight - 70 - 10
}
});
new FloatingButton({
text: '技术支持',
link: 'https://example.com/support',
initialPosition: {
x: (window.innerWidth - 190) / 2,
y: (window.innerHeight - 70) / 2
}
});
可拖动按钮
new FloatingButton({
text: '在线咨询',
link: 'https://example.com/chat',
cursor: 'move'
});
配置选项
ButtonConfig 接口
interface ButtonConfig {
text: string;
link: string;
style?: {
backgroundColor?: string;
color?: string;
fontSize?: string;
padding?: string;
borderRadius?: string;
boxShadow?: string;
[key: string]: any;
};
icon?: {
type: 'fontawesome' | 'url' | 'base64';
value: string;
width?: string;
height?: string;
};
initialPosition?: {
x: number;
y: number;
};
visible?: boolean;
debug?: boolean;
cursor?: 'pointer' | 'move' | 'default';
hoverScale?: number;
onClick?: (event: MouseEvent) => void;
}
默认配置
const defaultConfig: ButtonConfig = {
text: '联系我们',
link: 'https://example.com',
style: {
position: 'fixed',
backgroundColor: '#ffffff',
color: '#1e88e5',
fontSize: '18px',
padding: '10px 20px',
borderRadius: '5px',
boxShadow: '0 2px 10px rgba(0,0,0,0.1)',
border: '1px solid #1e88e5',
cursor: 'pointer',
zIndex: '9999',
display: 'flex',
alignItems: 'center',
gap: '10px',
transition: 'all 0.2s ease'
},
icon: {
type: 'fontawesome',
value: 'fas fa-comments',
width: '18px',
height: '18px'
},
initialPosition: {
x: 10,
y: window.innerHeight - 70 - 10
},
visible: true,
debug: false,
cursor: 'pointer',
hoverScale: 1.05
};
特性
- 默认显示在页面左下角
- 支持自定义样式和图标
- 支持 Font Awesome 图标、图片图标和 Base64 图标
- 支持自定义位置和拖动
- 支持悬停效果和动画
- 支持调试模式
- 支持自定义点击事件
- 响应式设计,适配不同屏幕尺寸
注意事项
- 使用 Font Awesome 图标时,需要先引入 Font Awesome 的 CSS 文件
- 图标大小默认与字体大小相同,可以通过
icon.width
和 icon.height
自定义
- 按钮默认显示在左下角,可以通过
initialPosition
自定义位置
- 所有样式属性都支持自定义覆盖
浏览器支持
- Chrome (推荐)
- Firefox
- Safari
- Edge
- Opera
许可证
MIT
配置选项
ButtonConfig 接口
interface ButtonConfig {
text?: string;
link?: string;
style?: {
position: 'fixed',
left?: string,
bottom?: string,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
padding: '0',
backgroundColor: 'rgba(240, 240, 240, 0.8)',
borderRadius: '10px',
border: '2px solid rgba(173, 216, 230, 0.8)',
boxShadow: '0 4px 10px rgba(0, 0, 0, 0.2)',
zIndex: '9999',
transition: 'transform 0.3s ease, box-shadow 0.3s ease',
cursor: 'pointer',
width: '190px',
height: '70px',
fontSize: '18px',
willChange: 'transform',
[key: string]: any;
};
icon?: {
type: 'url' | 'base64' | 'fontawesome';
value: string;
width?: string;
height?: string;
};
position?: string;
offset?: number;
visible?: boolean;
debug?: boolean;
cursor?: 'pointer' | 'move' | 'default';
hoverScale?: number;
onClick?: () => void;
initialPosition?: {
x: number;
y: number;
};
}
默认样式
- 位置:左下角,距离左边和底部各 10px
- 尺寸:190px × 70px
- 背景色:半透明浅灰色(rgba(240, 240, 240, 0.8))
- 边框:2px 半透明浅蓝色(rgba(173, 216, 230, 0.8))
- 阴影:0 4px 10px rgba(0, 0, 0, 0.2)
- 文字大小:18px
- 图标大小:默认与文字大小相同
悬浮效果
- 背景:从浅蓝色到深蓝色的渐变(linear-gradient(to bottom, #e8f7ff, #d1eaff))
- 边框:无
- 阴影:0px 0px 15px rgba(0, 0, 0, 0.3)
- 缩放:1.1 倍
示例
const button = new FloatingButton({
text: '联系我们',
position: 'bottom|right',
offset: 20
});
const buttonWithIcon = new FloatingButton({
text: '在线客服',
icon: {
type: 'fontawesome',
value: 'fas fa-headset'
},
style: {
fontSize: '20px'
}
});
const customButton = new FloatingButton({
text: '微信客服',
icon: {
type: 'url',
value: './wechat-icon.png',
width: '24px'
},
style: {
backgroundColor: '#f0f0f0',
color: '#333333'
},
position: 'center|right'
});