Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
ivy
Ivy Ui
提交
37dde3aa
I
Ivy Ui
项目概览
ivy
/
Ivy Ui
10 个月 前同步成功
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
I
Ivy Ui
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
37dde3aa
编写于
12月 01, 2022
作者:
fly丶知秋
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
WIP: 优化轮播图组件
上级
a26c8f12
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
114 addition
and
66 deletion
+114
-66
components/src/components/ivy-carousel/ivy-carousel.css
components/src/components/ivy-carousel/ivy-carousel.css
+1
-1
components/src/components/ivy-carousel/ivy-carousel.tsx
components/src/components/ivy-carousel/ivy-carousel.tsx
+46
-41
components/src/index.html
components/src/index.html
+11
-22
components/src/utils/utils.ts
components/src/utils/utils.ts
+56
-2
未找到文件。
components/src/components/ivy-carousel/ivy-carousel.css
浏览文件 @
37dde3aa
...
...
@@ -32,6 +32,6 @@
cursor
:
pointer
;
}
.indicator-item
[
active
]
{
.indicator-item
[
data-
active
]
{
background-color
:
#409eff
;
}
components/src/components/ivy-carousel/ivy-carousel.tsx
浏览文件 @
37dde3aa
import
{
Component
,
Host
,
h
,
Element
,
State
,
Prop
,
Watch
}
from
'
@stencil/core
'
;
import
{
getBrotherElements
}
from
'
../../utils/utils
'
;
import
{
Component
,
Host
,
h
,
Element
,
State
,
Prop
,
Watch
,
writeTask
}
from
'
@stencil/core
'
;
import
{
findElementsDownward
,
getBrotherElements
}
from
'
../../utils/utils
'
;
@
Component
({
tag
:
'
ivy-carousel
'
,
...
...
@@ -8,6 +8,9 @@ import { getBrotherElements } from '../../utils/utils';
})
export
class
IvyCarousel
{
@
Element
()
root
:
HTMLElement
;
@
State
()
slotEl
:
Element
;
@
State
()
slotChildren
:
HTMLElement
[]
=
[];
@
State
()
carouselItemList
:
NodeListOf
<
Element
>
;
...
...
@@ -26,15 +29,15 @@ export class IvyCarousel {
const
className
=
target
.
getAttribute
(
'
class
'
);
console
.
log
(
nodeName
,
className
);
if
(
nodeName
===
'
A
'
&&
className
===
'
indicator-item
'
)
{
const
index
=
target
.
getAttribute
(
'
index
'
);
const
index
=
target
.
getAttribute
(
'
data-
index
'
);
const
wrap
=
this
.
root
.
shadowRoot
.
querySelector
(
'
.wrap
'
);
const
children
=
wrap
.
children
;
console
.
log
(
wrap
,
index
,
children
);
(
wrap
as
any
).
style
.
transform
=
`translateX(-
${(
Number
(
index
)
/
children
.
length
)
*
100
}
%)`
;
target
.
setAttribute
(
'
active
'
,
''
);
target
.
setAttribute
(
'
data-
active
'
,
''
);
const
itemList
=
getBrotherElements
(
target
);
itemList
.
forEach
(
c
=>
{
c
.
removeAttribute
(
'
active
'
);
c
.
removeAttribute
(
'
data-
active
'
);
});
}
}
...
...
@@ -44,52 +47,54 @@ export class IvyCarousel {
<
Host
>
<
div
class
=
"wrap"
></
div
>
<
div
class
=
"wrap__hide"
>
<
slot
></
slot
>
<
slot
ref
=
{
el
=>
(
this
.
slotEl
=
el
)
}
></
slot
>
</
div
>
<
div
class
=
"indicator"
>
<
slot
name
=
"indicator"
>
<
div
class
=
"indicator-default"
onClick
=
{
this
.
handlerClick
.
bind
(
this
)
}
></
div
>
<
div
class
=
"indicator-default"
onClick
=
{
this
.
handlerClick
.
bind
(
this
)
}
>
{
this
.
slotChildren
.
map
((
_c
,
i
)
=>
{
if
(
Number
(
this
.
defaultActive
)
==
i
+
1
)
{
return
<
a
data
-
active
data
-
index
=
{
i
+
1
}
class
=
"indicator-item"
></
a
>;
}
else
{
return
<
a
data
-
index
=
{
i
+
1
}
class
=
"indicator-item"
></
a
>;
}
})
}
</
div
>
</
slot
>
</
div
>
</
Host
>
);
}
componentDidLoad
()
{
const
carouselItemList
=
this
.
root
.
querySelectorAll
(
'
ivy-carousel-item
'
);
const
len
=
carouselItemList
.
length
;
const
firstChild
:
any
=
carouselItemList
.
item
(
0
).
cloneNode
(
true
);
const
lastChild
:
any
=
carouselItemList
.
item
(
len
-
1
).
cloneNode
(
true
);
console
.
log
(
carouselItemList
,
firstChild
,
lastChild
);
const
tmpBox
=
document
.
createElement
(
'
div
'
);
const
itemFlex
=
(
1
/
(
len
+
2
))
*
100
;
firstChild
.
style
.
flex
=
`0 0
${
itemFlex
}
%`
;
lastChild
.
style
.
flex
=
`0 0
${
itemFlex
}
%`
;
tmpBox
.
appendChild
(
firstChild
);
carouselItemList
.
forEach
(
c
=>
{
c
.
style
.
flex
=
`0 0
${
itemFlex
}
%`
;
tmpBox
.
appendChild
(
c
);
});
tmpBox
.
appendChild
(
lastChild
);
const
wrap
=
this
.
root
.
shadowRoot
.
querySelector
(
'
.wrap
'
);
if
(
wrap
)
{
(
wrap
as
any
).
style
.
width
=
`
${(
carouselItemList
.
length
+
2
)
*
100
}
%`
;
wrap
.
innerHTML
=
tmpBox
.
innerHTML
;
(
wrap
as
any
).
style
.
transform
=
`translateX(-
${(
Number
(
this
.
defaultActive
)
/
(
len
+
2
))
*
100
}
%)`
;
}
componentWillLoad
()
{
writeTask
(()
=>
{
this
.
slotChildren
=
findElementsDownward
(
this
.
root
,
'
ivy-carousel-item
'
);
// this.slotChildren.forEach(c => {
// console.log(c.assignedSlot);
// });
// const tmp = (this.slotEl as any).assignedElements();
// console.log(tmp);
const
carouselItemList
=
this
.
root
.
querySelectorAll
(
'
ivy-carousel-item
'
);
const
len
=
carouselItemList
.
length
;
const
firstChild
:
any
=
carouselItemList
.
item
(
0
).
cloneNode
(
true
);
const
lastChild
:
any
=
carouselItemList
.
item
(
len
-
1
).
cloneNode
(
true
);
const
tmpBox
=
document
.
createElement
(
'
div
'
);
const
itemFlex
=
(
1
/
(
len
+
2
))
*
100
;
firstChild
.
style
.
flex
=
`0 0
${
itemFlex
}
%`
;
lastChild
.
style
.
flex
=
`0 0
${
itemFlex
}
%`
;
tmpBox
.
appendChild
(
firstChild
);
carouselItemList
.
forEach
(
c
=>
{
c
.
style
.
flex
=
`0 0
${
itemFlex
}
%`
;
tmpBox
.
appendChild
(
c
);
});
const
indicatorDefault
=
this
.
root
.
shadowRoot
.
querySelector
(
'
.indicator-default
'
);
if
(
indicatorDefault
)
{
let
str
=
''
;
for
(
let
i
=
0
;
i
<
len
;
i
++
)
{
if
(
Number
(
this
.
defaultActive
)
==
i
+
1
)
{
str
+=
`<a active index="
${
i
+
1
}
" class="indicator-item"></a>`
;
}
else
{
str
+=
`<a index="
${
i
+
1
}
" class="indicator-item"></a>`
;
}
tmpBox
.
appendChild
(
lastChild
);
const
wrap
=
this
.
root
.
shadowRoot
.
querySelector
(
'
.wrap
'
);
if
(
wrap
)
{
(
wrap
as
any
).
style
.
width
=
`
${(
carouselItemList
.
length
+
2
)
*
100
}
%`
;
wrap
.
innerHTML
=
tmpBox
.
innerHTML
;
(
wrap
as
any
).
style
.
transform
=
`translateX(-
${(
Number
(
this
.
defaultActive
)
/
(
len
+
2
))
*
100
}
%)`
;
}
indicatorDefault
.
innerHTML
=
str
;
}
});
}
}
components/src/index.html
浏览文件 @
37dde3aa
...
...
@@ -54,27 +54,19 @@
<div
style=
"min-height: 200px"
>
</div>
<div
class=
"margin-top"
>
<ivy-pager
class=
"pager"
></ivy-pager>
<ivy-button
id=
"btn"
>
更改进度条进度
</ivy-button>
</div>
<div
style=
"min-height: 200px"
>
<ivy-progress
value=
"30"
id=
"progress1"
></ivy-progress>
<ivy-progress
value=
"50"
class=
"margin-top"
></ivy-progress>
<ivy-progress
value=
"70"
class=
"margin-top"
></ivy-progress>
<ivy-progress
value=
"100"
class=
"margin-top"
></ivy-progress>
<div
style=
"height: 200px;"
></div>
<div>
<ivy-carousel>
<ivy-carousel-item>
1
</ivy-carousel-item>
<ivy-carousel-item>
2
</ivy-carousel-item>
<ivy-carousel-item>
3
</ivy-carousel-item>
<ivy-carousel-item>
4
</ivy-carousel-item>
</ivy-carousel>
</div>
<div
style=
"min-height: 200px"
>
<ivy-progress
value=
"70"
color=
"blue"
></ivy-progress>
<ivy-progress
value=
"70"
color=
"#afedf4"
class=
"margin-top"
></ivy-progress>
<ivy-progress
value=
"70"
color=
"green"
class=
"margin-top"
></ivy-progress>
<ivy-progress
value=
"70"
color=
"red"
class=
"margin-top"
></ivy-progress>
</div>
<div
style=
"min-height: 200px"
>
<ivy-progress
value=
"70"
color=
"blue"
show-text
></ivy-progress>
<ivy-progress
value=
"70"
color=
"#afedf4"
show-text
class=
"margin-top"
></ivy-progress>
<ivy-progress
value=
"70"
color=
"green"
show-text
class=
"margin-top"
></ivy-progress>
<ivy-progress
value=
"70"
color=
"red"
show-text
class=
"margin-top"
></ivy-progress>
</div>
</body>
<script>
...
...
@@ -85,10 +77,7 @@
console
.
log
(
this
.
pager
)
})
const
valueList
=
[
10
,
30
,
50
,
70
,
90
,
100
]
document
.
querySelector
(
'
#btn
'
).
addEventListener
(
'
click
'
,
()
=>
{
document
.
querySelector
(
'
#progress1
'
).
setValue
(
Math
.
floor
(
Math
.
random
()
*
100
))
})
</script>
...
...
components/src/utils/utils.ts
浏览文件 @
37dde3aa
export
function
format
(
first
:
string
,
middle
:
string
,
last
:
string
):
string
{
return
(
first
||
''
)
+
(
middle
?
`
${
middle
}
`
:
''
)
+
(
last
?
`
${
last
}
`
:
''
);
export
type
ScrollElement
=
HTMLElement
|
Window
;
const
defaultRoot
=
window
;
function
isElement
(
node
:
Element
)
{
const
ELEMENT_NODE_TYPE
=
1
;
return
node
.
tagName
!==
'
HTML
'
&&
node
.
tagName
!==
'
BODY
'
&&
node
.
nodeType
===
ELEMENT_NODE_TYPE
;
}
/**
...
...
@@ -152,6 +157,20 @@ export const findBrothersElements = (self: HTMLElement, nodeName: string, except
return
res
;
};
/**
* 获取slot的子元素
* @param slot slot 元素
* @param tag 筛选的元素名称
* @returns
*/
export
const
getAssignedElements
=
(
slot
:
HTMLSlotElement
,
tag
?:
string
)
=>
{
const
tmp
=
slot
.
assignedElements
();
if
(
tag
)
{
return
tmp
.
filter
(
c
=>
c
.
tagName
.
toLowerCase
()
===
tag
);
}
return
tmp
;
};
/**
* 颜色叠加
* @param {String} c1 颜色1-HEX格式
...
...
@@ -175,3 +194,38 @@ export const colorBlend = (c1: string, c2: string, ratio: number): string => {
b
=
''
+
(
b
||
0
).
toString
(
16
);
return
`#
${
r
}${
g
}${
b
}
`
;
};
export
const
throttle
=
(
func
:
Function
,
delay
=
0
,
atLeast
:
number
=
200
)
=>
{
let
timer
:
any
=
null
;
let
lastRun
=
0
;
return
(...
args
:
any
)
=>
{
const
now
=
+
new
Date
();
if
(
timer
)
{
clearTimeout
(
timer
);
}
if
(
now
-
lastRun
>
atLeast
)
{
lastRun
=
now
;
func
.
apply
(
this
,
args
);
}
else
{
timer
=
setTimeout
(()
=>
{
func
.
apply
(
this
,
args
);
},
delay
);
}
};
};
const
overflowScrollReg
=
/scroll|auto/i
;
// 获取滚动的父元素
export
function
getScrollParent
(
el
:
Element
,
root
:
ScrollElement
|
undefined
=
defaultRoot
)
{
let
node
=
el
;
while
(
node
&&
node
!==
root
&&
isElement
(
node
))
{
const
{
overflowY
}
=
window
.
getComputedStyle
(
node
);
if
(
overflowScrollReg
.
test
(
overflowY
))
{
return
node
;
}
node
=
node
.
parentNode
as
Element
;
}
return
root
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录