1. Component Introduction Affix components are used to fix page elements in a specific visual area. 1.1 Properties
1.2 Events
2. Source Code Analysis 2.1 template<template> <div ref="root" class="el-affix" :style="rootStyle"> <div :class="{'el-affix--fixed': state.fixed}" :style="affixStyle"> <slot></slot> </div> </div> </template> The template part is very simple, receiving content through slot 2.2 script// Some core codes, the code order is adjusted setup(props, { emit }) { // target container ref const target = ref(null) // Fix ref, and cooperate with the ref attribute in template to get the HTML element const root = ref(null) // Scroll container ref const scrollContainer = ref(null) // Fixed state const state = reactive({ fixed: false, height: 0, // height of root width: 0, // width of root scrollTop: 0, // scrollTop of documentElement clientHeight: 0, // clientHeight of documentElement transform: 0, }) onMounted(() => { // Determine the target container based on the incoming target if (props.target) { target.value = document.querySelector(props.target) if (!target.value) { throw new Error(`target is not existed: ${props.target}`) } } else { target.value = document.documentElement } // According to the fixed element, find the scroll container upwards scrollContainer.value = getScrollContainer(root.value) // Listen for the scroll event of the scroll container on(scrollContainer.value, 'scroll', onScroll) // Listen for the resize event of the fixed element addResizeListener(root.value, updateState) }) // The response function of the scroll container's scroll event const onScroll = () => { // Update the fixed state updateState() emit('scroll', { scrollTop: state.scrollTop, fixed: state.fixed, }) } // Update the fixed state function const updateState = () => { const rootRect = root.value.getBoundingClientRect() const targetRect = target.value.getBoundingClientRect() state.height = rootRect.height state.width = rootRect.width state.scrollTop = scrollContainer.value === window ? document.documentElement.scrollTop : scrollContainer.value.scrollTop state.clientHeight = document.documentElement.clientHeight if (props.position === 'top') { if (props.target) { const difference = targetRect.bottom - props.offset - state.height // targetRect.bottom > 0 means that the fixed pin is always kept in the container and hidden if it exceeds the range state.fixed = props.offset > rootRect.top && targetRect.bottom > 0 // Used to handle the scenario: During the scrolling process, if the visible area of the target container is not enough to display the entire pin, the pin should be offset accordingly to display only part of it state.transform = difference < 0 ? difference : 0 } else { state.fixed = props.offset > rootRect.top } } else { if (props.target) { const difference = state.clientHeight - targetRect.top - props.offset - state.height state.fixed = state.clientHeight - props.offset < rootRect.bottom && state.clientHeight > targetRect.top state.transform = difference < 0 ? -difference : 0 } else { state.fixed = state.clientHeight - props.offset < rootRect.bottom } } } // Monitor the fixed state change and emit change events watch(() => state.fixed, () => { emit('change', state.fixed) }) // Calculate the property and automatically update the style of the nail according to the state of the nail const affixStyle = computed(() => { if (!state.fixed) { return } const offset = props.offset ? `${props.offset}px` : 0 const transform = state.transform ? `translateY(${state.transform}px)` : '' return { height: `${state.height}px`, width: `${state.width}px`, top: props.position === 'top' ? offset : '', bottom: props.position === 'bottom' ? offset : '', transform: transform, zIndex: props.zIndex, } }) } 2.3 Implementation summary:
This is the end of this article about Element Plus’ implementation of Affix pinning. For more relevant Element Affix pinning content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A collection of possible problems when migrating sqlite3 to mysql
>>: VMware and CentOS system installation method to reset the root password
What is ZooKeeper ZooKeeper is a top-level projec...
At the beginning of this article, I would like to ...
See: https://www.jb51.net/article/112612.htm Chec...
There are many scripts on the Internet that use e...
I recently read Yahoo's 34 Golden Rules and le...
1. Environmental Preparation Tencent Cloud Server...
This article describes how to install lamp-php7.0...
Table of contents 1. Project Requirements Second,...
This article example shares the specific code of ...
Preface Vue Router is the official routing manage...
In an article a long time ago, I talked about the...
Table of contents Problem Description Principle A...
Table of contents What is recursion and how does ...
js interesting countdown case, for your reference...
1. Overview The so-called life cycle function is ...