2024年前端最新《Vue3+TS》开发一个自己的起始页(1),腾讯T3大牛手把手教你

最后

前15.PNG

前16.PNG

由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

},

setup(props, context) {

// 表单DOM

const threeSBgRef: any = ref(null);

// 获取vuex信息

const store: Store = useStore();

// 背景图路径

const imgSrcPath = ref(“”);

// 背景图状态,false-隐藏,true-显示

const showImg = ref(false);

// 背景图信息

const bagImg = computed(() => store.getters[“basic/basic/getBg”]);

/**

  • 背景图DOM

  • @returns {HTMLElement} 背景图DOM

*/

async function getImgDom(): Promise {

return new Promise((resolve) => {

nextTick(() => {

resolve(threeSBgRef.value);

});

});

}

watch(

() => bagImg.value,

async (value) => {

if (Array.isArray(value)) {

return false;

}

const img: HTMLElement = await getImgDom();

img.onload = () => {

showImg.value = true;

};

imgSrcPath.value = value.path;

}

);

/**

  • 右击

*/

function handleRightClick(): void | boolean {

if (imgSrcPath.value === “”) return false;

context.emit(“changeShortcuts”, !props.shortcuts);

}

/**

  • 左击

*/

function handleLeftClick(): void | boolean {

if (!props.glass) return false;

context.emit(“changeGlass”, false);

}

return {

imgSrc: computed(() =>

imgSrcPath.value === “”

? null
require( ../../../../../src/assets/bg/${imgSrcPath.value})

),

showImg,

threeSBgRef,

handleRightClick,

handleLeftClick,

};

},

});

另外,这里还有一个骨架屏的效果,就是当背景图还没有被加载的时候,整个背景图处于灰色,并且有一个从左往右的动效,代表正在加载中,骨架屏效果代码如下:

.threeS-loading {

background-color: #f2f2f2;

background: linear-gradient(

100deg,

rgba(255, 255, 255, 0) 40%,

rgba(255, 255, 255, 0.5) 50%,

rgba(255, 255, 255, 0) 60%

)

#f6f6f6;

background-size: 200% 100%;

background-position-x: 120%;

animation: 1s loading ease-in-out infinite;

}

@keyframes loading {

to {

background-position-x: -20%;

}

}

时钟组件


这个组件的难点在于实现电子数字的格式,这个是最复杂的,而时间的获取就是基于浏览器Date对象,这个很简单,获取时间的方法部分如下:

/**

  • 获得时间

  • @returns {String} 当前时间

*/

function getTime(): string {

const date = new Date();

const hour = date.getHours();

const minute = date.getMinutes();

return (

(hour >= 10 ? hour : “0” + hour) +

“:” +

(minute >= 10 ? minute : “0” + minute)

);

}

当获取到时间以后,需要将转成电子格式的,这部分的原理就是先拼出一个电子格式的8,然后,不同的数字只是去掉8中的某一个笔划,这样就达到了0-9的数字,这里我挑一部分CSS代码

.digits div span {

opacity: 0;

position: absolute;

-webkit-transition: 0.25s;

-moz-transition: 0.25s;

transition: 0.25s;

}

.digits div span:before,

.digits div span:after {

content: “”;

position: absolute;

width: 0;

height: 0;

border: @fontSize solid transparent;

}

.digits .d1 {

height: @fontSize;

width: 16px;

top: 0;

left: 6px;

}

.digits .d1:before {

border-width: 0 @fontSize @fontSize 0;

border-right-color: inherit;

left: -@fontSize;

}

.digits .d1:after {

border-width: 0 0 @fontSize @fontSize;

border-left-color: inherit;

right: -@fontSize;

}

.digits .d2 {

height: @fontSize;

width: 16px;

top: 24px;

left: 6px;

}

.digits .d2:before {

border-width: 3px 4px 2px;

border-right-color: inherit;

left: -8px;

}

.digits .d2:after {

border-width: 3px 4px 2px;

border-left-color: inherit;

right: -8px;

}

这里是1和2的组成,通过类似的方法将0-9的CSS都写完,之后获取到时间,在填入的时候判断需要填入的是哪个数字就行:

/**

  • 填入时间

  • @param {String} time - 时间

  • @param {HTMLElement} timerClock - 填入时间的DOM

  • @returns 无

*/

function clocknum(time: string, timerClock: HTMLElement): void {

timerClock.innerHTML = “”;

var html = “”;

var strarr: any[] = time.toString().split(“”);

var digit_to_name = “zero one two three four five six seven eight nine”.split(

" "

);

for (var i = 0; i < strarr.length; i++) {

if (strarr[i] == “:”) {

html += ‘

’;

} else {

var clasname = digit_to_name[strarr[i]];

html +=

‘<div class="’ +

clasname +

‘">’ +

’ +

’ +

’ +

’ +

’ +

’ +

’ +

“”;

}

}

timerClock.innerHTML = html;

}

这样时间组件就算基本完成了,使用的时候只需要引入主页,放在对应的位置就可以了;

搜索组件


搜索组件一共分为两部分,需要单独实现,分别为:搜索框,搜索历史(热门搜索);

搜索框

这个其实简单,其实就是一个输入框,并且在其上附加了一个聚焦事件,用于和背景组件的联动,确认搜索的时候根据搜索类型打开对应的页签就可以了,并且确认搜索的时候,将该条搜索条件作为记录存储到本地缓存中,所谓历史记录的一部分,部分实现代码如下:

:class="[

‘threeS-search-container’,

{ ‘threeS-search-hover’: mouseEnter },

{ ‘threeS-search-active’: glass },

]"

@mouseenter=“handleMouseEvent”

@mouseleave=“handleMouseEvent”

<input

class=“threeS-search-input”

type=“text”

:placeholder=“glass ? ‘’ : ‘搜索内容’”

@focus=“handleFocus”

v-model=“searchValue”

@keyup.enter=“handleSearch”

/>

<button

v-if=“glass”

class=“icon iconfont icon-magnifier search-icon”

@click=“handleSearch”

:class="[

‘threeS-search-type-container’,

{ ‘threeS-type-focus’: glass },

]"

class=“threeS-search-type”

:class=“[{ ‘is-active’: item.id === searchType }]”

v-for=“item in searchTypeList”

:key=“item.id”

@click=“handleSearchType(item)”

{{ item.name }}

这个是HTML部分,从结构上看没有什么复杂的,只是搜索的按钮需要融入搜索框的背景之中,不显突兀;

const searchTypeList: searchType[] = [

{

name: “百度”,

id: “baidu”,

url: “https://www.baidu.com/s?wd=”,

},

{

name: “谷歌”,

id: “google”,

url: “https://www.google.com/search?q=”,

},

{

name: “必应”,

id: “bing”,

url: “https://cn.bing.com/search?q=”,

},

{

name: “淘宝”,

id: “taobao”,

url: “https://s.taobao.com/search?q=”,

},

];

const searchType: Ref = ref(searchTypeList[0].id);

const searchUrl: Ref = ref(searchTypeList[0].url);

function handleSearchType(item: searchType): void {

searchType.value = item.id;

searchUrl.value = item.url;

}

这个一部分搜索类型时的代码,这部分也不复杂,但是这部分又很关键,因为每个平台的搜索地址前缀到底是什么样的,网络上查了很久都没查到合理的答案,只能自己试,试了好久才发现;

历史记录

历史记录这一块主要的知识点在Vue部分其实就是一个for循环,通过对数据的便利将其展示出来,对了,还有有一个,就是数据的来源,这里的一部分是来自于后台接口,一部分是来自于本地的localstage,这里存在一个数据合并的问题以及清除历史记录时需要对本地的localstage进行同步清理,下面是部分代码

// ---------- 历史信息 start ----------

const getHistoryInfo: any = computed(

() => store.getters[“basic/basic/getHistory”]

);

watch(

() => getHistoryInfo.value,

(value) => {

historyList.value = value;

}

);

const updateHistory: ActionMethod = (history: urlInterface[]) =>

store.dispatch(“basic/basic/updateHistoryInfo”, history);

/**

  • 清除历史记录

  • @param {Object} item 待清除的项

*/

function handleClearHistory(item: urlInterface): void {

const newHistoryInfo: urlInterface[] = getHistoryInfo.value.filter(

(el: urlInterface) => {

return el.url !== item.url;

}

);

// 更新

updateHistory(newHistoryInfo);

}

function goHistoryUrl(item: urlInterface): void {

window.open(item.url);

}

/**

  • 清除全部历史记录

*/

function clearHistory() {

// 更新

updateHistory([]);

}

// ---------- 历史信息 end ----------

快捷入口


快捷入口的难点其实在于ICON的展示,至于关于Vue的知识点其实也同样跟上面一样是一个v-for的循环,将数据遍历并展示出来,并为其增加点击事件,点击后直接打开新窗口;

至于ICON的难点在于一开始使用的是iconfont的Font class模式,但是后来发现Font class模式有一个最大的缺点:无法展示多色,因此整个icon都是一个个单色的方块,后来通过仔细了解字体模式,发现了它SVG的用法,使用SVG可以将其展示成多色,当然这个模式也有一个缺点,就是对浏览器的兼容没那么好,但是,IE什么的本来就不再考虑了,以下是部分代码:

    • {{ item.name }}
    • 30
      点赞
    • 18
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值