Commit df0d23cf authored by hucy's avatar hucy

js练习

parent 841aaffb
<!-- <!--
* 轮播图 * 动态echar轮播图
--> -->
<script setup lang="ts"> <script setup lang="ts">
import { import { ref, reactive, onBeforeUnmount, watch, nextTick, markRaw } from 'vue';
ref,
reactive,
onMounted,
onBeforeUnmount,
watch,
nextTick,
markRaw,
} from 'vue';
import { isEmpty } from 'src/common/utils'; import { isEmpty } from 'src/common/utils';
import * as echarts from 'echarts'; import * as echarts from 'echarts';
...@@ -28,7 +20,7 @@ const props = withDefaults(defineProps<Props>(), { ...@@ -28,7 +20,7 @@ const props = withDefaults(defineProps<Props>(), {
navigation: false, navigation: false,
}); });
const DOM_PIE_KEY = 'chart-pie-dom-'; const DOM_PIE_KEY = 'chart-dom-';
const pointActive = ref<any>(1); const pointActive = ref<any>(1);
const lastIndex = ref(0); const lastIndex = ref(0);
...@@ -47,10 +39,6 @@ watch( ...@@ -47,10 +39,6 @@ watch(
} }
); );
onMounted(() => {
//
});
onBeforeUnmount(() => { onBeforeUnmount(() => {
offTimer(); offTimer();
}); });
...@@ -101,7 +89,6 @@ function listChange() { ...@@ -101,7 +89,6 @@ function listChange() {
if (condition && option) { if (condition && option) {
// console.log('设置了', myChart); // console.log('设置了', myChart);
myChart.resize();
myChart.clear(); myChart.clear();
myChart.setOption(option); myChart.setOption(option);
} }
...@@ -218,7 +205,7 @@ function resetIndex() { ...@@ -218,7 +205,7 @@ function resetIndex() {
} }
</script> </script>
<template> <template>
<div class="carousel-box"> <div class="chart-carousel-box">
<div class="carousel"> <div class="carousel">
<div class="carousel-content"> <div class="carousel-content">
<div class="carousel-item-box"> <div class="carousel-item-box">
...@@ -256,13 +243,15 @@ function resetIndex() { ...@@ -256,13 +243,15 @@ function resetIndex() {
@keyframes opacity-in { @keyframes opacity-in {
0% { 0% {
opacity: 0.5; opacity: 0.5;
transform: rotateY(180deg);
} }
100% { 100% {
opacity: 1; opacity: 1;
transform: rotateY(0);
} }
} }
.carousel-box { .chart-carousel-box {
width: 100%; width: 100%;
height: 100%; height: 100%;
} }
......
...@@ -3,7 +3,7 @@ import BaiduMap from './BaiduMap.vue'; ...@@ -3,7 +3,7 @@ import BaiduMap from './BaiduMap.vue';
import MyForm from './MyForm.vue'; import MyForm from './MyForm.vue';
import MyTooltip from './MyTooltip.vue'; import MyTooltip from './MyTooltip.vue';
import CanvasStaticRoute from './canvas-static-route/CanvasStaticRoute.vue'; import CanvasStaticRoute from './canvas-static-route/CanvasStaticRoute.vue';
import CarouselWarp from './carousel-warp/CarouselWarp.vue'; import ChartCarouselWarp from './chart-carousel-warp/ChartCarouselWarp.vue';
export { export {
DateTimePick as ComDateTimePick, DateTimePick as ComDateTimePick,
...@@ -11,7 +11,7 @@ export { ...@@ -11,7 +11,7 @@ export {
MyForm as ComForm, MyForm as ComForm,
MyTooltip as ComTooltip, MyTooltip as ComTooltip,
CanvasStaticRoute as ComCanvasStaticRoute, CanvasStaticRoute as ComCanvasStaticRoute,
CarouselWarp as ComCarouselWarp, ChartCarouselWarp as ComChartCarouselWarp,
}; };
export default { export default {
...@@ -20,5 +20,5 @@ export default { ...@@ -20,5 +20,5 @@ export default {
MyForm, MyForm,
MyTooltip, MyTooltip,
CanvasStaticRoute, CanvasStaticRoute,
CarouselWarp, ChartCarouselWarp,
}; };
// app global css in SCSS form // app global css in SCSS form
a:link {
color: $primary;
} /* 未访问的链接 */
a:visited {
color: $primary;
} /* 已访问的链接 */
a:hover {
opacity: 0.8;
} /* 鼠标移动到链接上 */
// 布局 // 布局
.center { .center {
display: flex; display: flex;
......
...@@ -10,14 +10,16 @@ export default { ...@@ -10,14 +10,16 @@ export default {
// import ArrayOutOfOrder from './element/ArrayOutOfOrder.vue'; // import ArrayOutOfOrder from './element/ArrayOutOfOrder.vue';
// import CanvasStaticRoute from './element/CanvasStaticRoute.vue'; // import CanvasStaticRoute from './element/CanvasStaticRoute.vue';
// import MorphBox from './element/MorphBox.vue'; // import MorphBox from './element/MorphBox.vue';
import CarouselWarp from './element/CarouselWarp.vue'; // import CarouselWarp from './element/CarouselWarp.vue';
import DataResponsive from './element/DataResponsive.vue';
</script> </script>
<template> <template>
<div class="page6 container-height"> <div class="page6 container-height">
<!-- <array-out-of-order /> --> <!-- <array-out-of-order /> -->
<!-- <canvas-static-route /> --> <!-- <canvas-static-route /> -->
<!-- <morph-box /> --> <!-- <morph-box /> -->
<carousel-warp /> <!-- <carousel-warp /> -->
<data-responsive />
</div> </div>
</template> </template>
......
...@@ -15,9 +15,11 @@ const COLORLIST = [ ...@@ -15,9 +15,11 @@ const COLORLIST = [
export const WarpTestData = [ export const WarpTestData = [
{ {
id: 1, id: 1,
seq: 1,
label: '1号', label: '1号',
url: require('./element/imgs/carousel-1.jpg'), url: require('./element/imgs/carousel-1.jpg'),
option: { option: {
animation: false,
color: COLORLIST, color: COLORLIST,
title: { title: {
text: '1号', text: '1号',
...@@ -46,9 +48,11 @@ export const WarpTestData = [ ...@@ -46,9 +48,11 @@ export const WarpTestData = [
}, },
{ {
id: 2, id: 2,
seq: 2,
label: '2号', label: '2号',
url: require('./element/imgs/carousel-2.jpg'), url: require('./element/imgs/carousel-2.jpg'),
option: { option: {
animation: false,
color: COLORLIST, color: COLORLIST,
title: { title: {
text: '2号', text: '2号',
...@@ -103,9 +107,11 @@ export const WarpTestData = [ ...@@ -103,9 +107,11 @@ export const WarpTestData = [
}, },
{ {
id: 3, id: 3,
seq: 3,
label: '3号', label: '3号',
url: require('./element/imgs/carousel-3.jpg'), url: require('./element/imgs/carousel-3.jpg'),
option: { option: {
animation: false,
color: COLORLIST, color: COLORLIST,
title: { title: {
text: '3号', text: '3号',
...@@ -146,9 +152,11 @@ export const WarpTestData = [ ...@@ -146,9 +152,11 @@ export const WarpTestData = [
}, },
{ {
id: 4, id: 4,
seq: 4,
label: '4号', label: '4号',
url: require('./element/imgs/carousel-4.jpg'), url: require('./element/imgs/carousel-4.jpg'),
option: { option: {
animation: false,
color: COLORLIST, color: COLORLIST,
title: { title: {
text: '4号', text: '4号',
...@@ -185,9 +193,11 @@ export const WarpTestData = [ ...@@ -185,9 +193,11 @@ export const WarpTestData = [
export const WarpTestData2 = [ export const WarpTestData2 = [
{ {
id: 1, id: 1,
seq: 1,
label: '1号', label: '1号',
url: require('./element/imgs/carousel-1.jpg'), url: require('./element/imgs/carousel-1.jpg'),
option: { option: {
animation: false,
color: COLORLIST, color: COLORLIST,
title: { title: {
text: '1号-测试数据2', text: '1号-测试数据2',
...@@ -216,9 +226,11 @@ export const WarpTestData2 = [ ...@@ -216,9 +226,11 @@ export const WarpTestData2 = [
}, },
{ {
id: 2, id: 2,
seq: 2,
label: '2号', label: '2号',
url: require('./element/imgs/carousel-2.jpg'), url: require('./element/imgs/carousel-2.jpg'),
option: { option: {
animation: false,
color: COLORLIST, color: COLORLIST,
title: { title: {
text: '2号-测试数据2', text: '2号-测试数据2',
...@@ -273,9 +285,11 @@ export const WarpTestData2 = [ ...@@ -273,9 +285,11 @@ export const WarpTestData2 = [
}, },
{ {
id: 3, id: 3,
seq: 3,
label: '3号-测试数据2', label: '3号-测试数据2',
url: require('./element/imgs/carousel-3.jpg'), url: require('./element/imgs/carousel-3.jpg'),
option: { option: {
animation: false,
color: COLORLIST, color: COLORLIST,
title: { title: {
text: '3号-测试数据2', text: '3号-测试数据2',
...@@ -316,9 +330,11 @@ export const WarpTestData2 = [ ...@@ -316,9 +330,11 @@ export const WarpTestData2 = [
}, },
{ {
id: 4, id: 4,
seq: 4,
label: '4号', label: '4号',
url: require('./element/imgs/carousel-4.jpg'), url: require('./element/imgs/carousel-4.jpg'),
option: { option: {
animation: false,
color: COLORLIST, color: COLORLIST,
title: { title: {
text: '4号-测试数据2', text: '4号-测试数据2',
...@@ -350,9 +366,11 @@ export const WarpTestData2 = [ ...@@ -350,9 +366,11 @@ export const WarpTestData2 = [
}, },
{ {
id: 5, id: 5,
seq: 5,
label: '5号', label: '5号',
url: require('./element/imgs/carousel-4.jpg'), url: require('./element/imgs/carousel-4.jpg'),
option: { option: {
animation: false,
color: COLORLIST, color: COLORLIST,
title: { title: {
text: '5号-测试数据2', text: '5号-测试数据2',
...@@ -397,9 +415,11 @@ export const WarpTestData2 = [ ...@@ -397,9 +415,11 @@ export const WarpTestData2 = [
export const WarpTestData3 = [ export const WarpTestData3 = [
{ {
id: 1, id: 1,
seq: 1,
label: '1号', label: '1号',
url: require('./element/imgs/carousel-1.jpg'), url: require('./element/imgs/carousel-1.jpg'),
option: { option: {
animation: false,
color: COLORLIST, color: COLORLIST,
title: { title: {
text: '1号-测试数据3', text: '1号-测试数据3',
......
<!-- <!--
* 轮播图 * 动态echar轮播图
--> -->
<script setup lang="ts"> <script setup lang="ts">
import { reactive, onMounted, onBeforeUnmount } from 'vue'; import { reactive, onMounted, onBeforeUnmount } from 'vue';
import Com from 'src/components'; import Com from 'src/components';
import { WarpTestData, WarpTestData2, WarpTestData3 } from '../config'; import { WarpTestData, WarpTestData2, WarpTestData3 } from '../config';
import * as echarts from 'echarts';
const state = reactive({ const state = reactive({
list: [] as any, list: [] as any,
times: 5000, times: 5000,
testChart: null as any,
}); });
onMounted(() => { onMounted(() => {
test111();
initData(); initData();
}); });
...@@ -45,65 +42,17 @@ function onSetToEnpty() { ...@@ -45,65 +42,17 @@ function onSetToEnpty() {
function onChangeTimers() { function onChangeTimers() {
state.times = 2000; state.times = 2000;
} }
function test111() {
let chartDom: any = document.getElementById('test111');
state.testChart = echarts.init(chartDom);
const option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
},
yAxis: {
type: 'value',
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'line',
},
],
};
state.testChart.setOption(option);
}
function onDestructionChart() {
state.testChart.dispose();
}
function onResetChart() {
test111();
}
function onResetData() {
const option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
},
yAxis: {
type: 'value',
},
series: [
{
data: [300, 300, 300, 300, 300, 300, 300],
type: 'line',
},
],
};
state.testChart.setOption(option);
}
function onView() {
console.log('查看 =', state.testChart);
}
</script> </script>
<template> <template>
<div class="fit"> <div class="fit">
<div class="my-warp-box"> <div class="my-warp-box">
<Com.CarouselWarp <Com.ChartCarouselWarp
navigation navigation
item-key="id" item-key="seq"
:list="state.list" :list="state.list"
:times="state.times" :times="state.times"
> >
</Com.CarouselWarp> </Com.ChartCarouselWarp>
</div> </div>
<div class="btns"> <div class="btns">
<q-btn <q-btn
...@@ -124,21 +73,10 @@ function onView() { ...@@ -124,21 +73,10 @@ function onView() {
<q-btn label="将数据置为空" @click="onSetToEnpty" color="primary" /> <q-btn label="将数据置为空" @click="onSetToEnpty" color="primary" />
<q-btn label="改变轮播时间" @click="onChangeTimers" color="orange" /> <q-btn label="改变轮播时间" @click="onChangeTimers" color="orange" />
</div> </div>
<q-btn label="销毁图表" @click="onDestructionChart" color="orange" />
<q-btn label="重绘图表" @click="onResetChart" color="orange" />
<q-btn label="重新设置数据" @click="onResetData" color="orange" />
<q-btn label="查看" @click="onView" color="orange" />
<div class="test111" id="test111"></div>
</div> </div>
</template> </template>
<style lang="scss" scoped> <style lang="scss" scoped>
.test111 {
width: 500px;
height: 500px;
border: 1px solid red;
}
.my-warp-box { .my-warp-box {
width: 500px; width: 500px;
height: 300px; height: 300px;
......
<!--
* vue3的数据响应性
-->
<script setup lang="ts">
import { ref, unref, reactive, toRaw } from 'vue';
import { useMessage } from 'src/common/hooks';
const { info } = useMessage();
const text = ref('夕阳西下');
const refObj = ref({
name: '张三',
age: 18,
});
const state = reactive({
name: '李四',
age: 49,
childs: {
name: '李白',
age: 111,
},
});
function onView1() {
console.log('查看1 =', text);
}
function onView2() {
console.log('查看2 =', refObj);
}
function onChange() {
text.value = '改变改变';
info(unref(text));
}
function onViewState() {
console.log('state', state);
}
function onToRawState() {
console.log('toRaw(state) =', toRaw(state));
}
function onViewDefineProperty() {
let o = {
name: '张三',
age: 18,
};
myreactive(o, 'name', 'zhang san');
console.log('o >>>>', o.name);
o.age = 20;
}
// 响应式函数
function myreactive(obj: any, key: any, value: any) {
Object.defineProperty(obj, key, {
get() {
console.log(`访问了${key}属性`);
return value;
},
set(val) {
console.log(`将${key}由->${value}->设置成->${val}`);
if (value !== val) {
value = val;
}
},
});
}
function onViewEffect() {
let name = '孙师傅';
let age = 18;
let money = 200;
let res1 = null;
let res2 = null;
let dep: any = new Set();
const effect1 = () => {
res1 = `${name},今年${age},有${money}元`;
};
const effect2 = () => {
res2 = `刚满${age}岁没多少年的${name},已经拥有了巨额存款${money}元`;
};
// 收集依赖
(function track() {
dep.add(effect1);
dep.add(effect2);
})();
console.log('dep >>>>', dep);
// 通知变了进行更新
function trigger() {
for (let item of dep) {
item();
}
}
trigger(); // 第一次执行,初始化
console.log(res1);
console.log(res2);
money = 400;
age = 40;
trigger(); // 数据更新
console.log(res1);
console.log(res2);
}
</script>
<template>
<div class="fit">
<div class="title">数据响应性</div>
<div>
<a href="https://juejin.cn/post/7001999813344493581" target="_blank"
>参考链接</a
>
</div>
<div class="q-mb-md">
<q-card>
<q-card-section>
<div>
<q-btn label="查看1" color="primary" @click="onView1" />
<q-btn label="查看2" color="primary" @click="onView2" />
<q-btn label="改变数据" color="primary" @click="onChange" />
</div>
<div>
<q-chip square>{{ text }}</q-chip>
</div>
</q-card-section>
</q-card>
</div>
<div class="q-mb-md">
<q-card>
<q-card-section>
<q-btn label="查看state" color="primary" @click="onViewState" />
<q-btn label="toRaw(state)" color="primary" @click="onToRawState" />
</q-card-section>
</q-card>
</div>
<div class="q-mb-md">
<q-card>
<q-card-section>
<div>
<q-chip square>Object.defineProperty</q-chip>
</div>
<div>
<q-btn label="查看" color="primary" @click="onViewDefineProperty" />
</div>
</q-card-section>
</q-card>
</div>
<div class="q-mb-md">
<q-card>
<q-card-section>
<div>
<q-btn
label="订阅与更新简单版"
color="primary"
@click="onViewEffect"
/>
</div>
</q-card-section>
</q-card>
</div>
</div>
</template>
<style lang="scss" scoped>
.title {
font-size: 22px;
color: #424242;
margin-bottom: $padding-md;
}
</style>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment