{"id":163,"date":"2022-10-25T20:37:10","date_gmt":"2022-10-25T12:37:10","guid":{"rendered":"http:\/\/weboob.top\/blog\/?p=163"},"modified":"2022-10-25T20:37:10","modified_gmt":"2022-10-25T12:37:10","slug":"%e8%8f%9c%e5%8d%95%e6%8c%87%e7%a4%ba%e9%a1%b5%e9%9d%a2%e8%81%94%e5%8a%a8demo","status":"publish","type":"post","link":"http:\/\/blog.weboob.top\/?p=163","title":{"rendered":"\u83dc\u5355\u6307\u793a\u9875\u9762\u8054\u52a8Demo"},"content":{"rendered":"\n<ul class=\"wp-block-list\"><li>\u6838\u5fc3\u601d\u8def\uff1a\u6839\u636eindex\uff0c\u6e32\u67d3\u83dc\u5355\u9009\u4e2d\u6837\u5f0f\uff0c\u4ee5\u53cacontent\u7684scroll\u5b9a\u4f4d\u3002<\/li><li>\u4e24\u6b65\u5b9e\u73b0\uff1a<ol><li>\u70b9\u51fb\u83dc\u5355\u65f6\uff0c\u901a\u8fc7<code>scrollIntoView<\/code>&nbsp;api \u5b9e\u73b0\u9875\u9762\u5143\u7d20\u7684\u5e73\u6ed1\u6eda\u52a8\u3002<\/li><li>\u6ce8\u518c\u6eda\u52a8\u4e8b\u4ef6\uff0c\u53cd\u5411\u540c\u6b65\u83dc\u5355\u9009\u4e2d\u6837\u5f0f<\/li><\/ol><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff08\u4ec5\u4f9b\u53c2\u8003\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script setup&gt;\nimport { reactive, ref} from \"vue\";\n\nconst blocks = ref(&#91;]);\nconst state = reactive({\n    menus:&#91;'\u83dc\u53551','\u83dc\u53552','\u83dc\u53553','\u83dc\u53554','\u83dc\u53555',],\n    current: 0,\n    blocks:&#91;],\n});\n\/\/=================== Methods ====================\n\/**\n * \u83dc\u5355\u70b9\u51fb\u4e8b\u4ef6\n * *\/\nconst handleClick = idx =&gt; {\n    state.current = idx;\n    \/\/ \u76ee\u6807\u6eda\u52a8\u5230\u53ef\u89c6\u533a\u57df\n    document.querySelectorAll('.block')&#91;idx].scrollIntoView({ block: 'start', behavior: \"smooth\"})\n};\n\/**\n * \u83b7\u53d6\u951a\u70b9\u6570\u7ec4\n * *\/\nconst setBlocks = (block) =&gt; {\n    blocks.value.push(block);\n};\n\/**\n * \u6eda\u52a8\u4e8b\u4ef6\n * *\/\nconst handleScroll = (e) =&gt; {\n    const block = document.querySelectorAll('.block');\n    \/\/ \u4ece\u540e\u5f80\u524d\u5224\u65ad\n    for (let i = block.length-1; i &gt;= 0 ; i--) {\n        const scrollView = e.target.scrollTop &gt;= block&#91;i].offsetTop;\n        if(scrollView){\n            state.current = i;\n            break;\n        }\n    }\n};\n&lt;\/script&gt;\n\n&lt;template&gt;\n&lt;div class=\"container\"&gt;\n    &lt;div class=\"header\"&gt;\n        &lt;h3&gt;scrollInTo\u793a\u4f8b&lt;\/h3&gt;\n    &lt;\/div&gt;\n    &lt;div class=\"left\"&gt;\n        &lt;ul&gt;\n            &lt;li\n                :class=\"{'selected' : state.current === index }\"\n                v-for=\"(item,index) in state.menus\"\n                @click=\"handleClick(index)\"\n            &gt;{{item}}&lt;\/li&gt;\n        &lt;\/ul&gt;\n    &lt;\/div&gt;\n    &lt;div class=\"right\" @scroll=\"handleScroll\"&gt;\n        &lt;div class=\"block\"\n             :ref=\"setBlocks\"\n             v-for=\"(item, index) in state.menus\"\n        &gt;\n            &lt;div class=\"title\"&gt;{{ item }}&amp;nbsp;\u6a21\u5757&lt;\/div&gt;\n            &lt;div class=\"content\"&gt;{{ item }}&amp;nbsp;\u5185\u5bb9&lt;\/div&gt;\n        &lt;\/div&gt;\n    &lt;\/div&gt;\n&lt;\/div&gt;\n&lt;\/template&gt;\n\n\n&lt;style scoped lang=\"less\"&gt;\n@select-color: #F73802;\nul,li{\n    margin: 0;\n    padding: 0;\n    list-style: none;\n}\n.container{\n    width: 100%;\n    height: 100%;\n    background: #C9D1DB40;\n    padding: 20px;\n    display: flex;\n    flex-flow: row wrap;\n    justify-content: space-between;\n    align-items: flex-start;\n    align-content: flex-start;\n    .header{\n        height: 60px;\n        width: 100%;\n        background: white;\n        box-shadow: 0 10px 10px #88888860;\n        margin-bottom: 20px;\n        padding: 5px 20px;\n        display: flex;\n        align-items: center;\n        border-radius: 8px;\n    }\n    .left{\n        width: 200px;\n        background: white;\n        border-radius: 8px;\n        box-shadow: 0 10px 10px #88888860;\n        padding: 20px;\n        position: sticky;\n        ul{\n            li{\n                color: #888888;\n                position: relative;\n                cursor: pointer;\n                &amp;:not(:last-child){\n                    margin-bottom: 10px;\n                }\n                &amp;:hover{\n                    color: @select-color;\n                    font-weight: bold;\n                }\n                &amp;.selected{\n                    color: @select-color;\n                    font-weight: bold;\n                    &amp;:before{\n                        content: \"\";\n                        width: 4px;\n                        height: 4px;\n                        border-radius: 50%;\n                        position: absolute;\n                        left: -8px;\n                        top: 50%;\n                        transform: translateY(-50%);\n                        background: @select-color;\n                    }\n                }\n\n            }\n        }\n\n    }\n    .right{\n        width: 350px;\n        height: 400px;\n        overflow-x: hidden;\n        overflow-y: auto;   \/\/ \u5b9e\u73b0\u6eda\u52a8\n        box-shadow: 0 10px 10px #88888860;\n        position: relative; \/\/ \u7528\u4e8e\u5b50\u5143\u7d20\u83b7\u53d6offsetTop\n        .block{\n            width: 100%;\n            border-radius: 8px;\n            background: white;\n            margin-bottom: 20px;\n            box-shadow: 0 10px 10px #88888860;\n            .title{\n                height: 30px;\n                border-bottom: 1px solid #c0c0c060;\n                display: flex;\n                align-items: center;\n                color: @select-color;\n                padding-left: 20px;\n            }\n            .content{\n                padding-left: 20px;\n                min-height: 150px;\n            }\n        }\n    }\n}\n&lt;\/style&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6838\u5fc3\u601d\u8def\uff1a\u6839\u636eindex\uff0c\u6e32\u67d3\u83dc\u5355\u9009\u4e2d\u6837\u5f0f\uff0c\u4ee5\u53cacontent\u7684scroll\u5b9a\u4f4d\u3002 \u4e24\u6b65\u5b9e\u73b0\uff1a \u70b9\u51fb\u83dc\u5355\u65f6\uff0c\u901a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-vue3"],"_links":{"self":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163"}],"version-history":[{"count":0,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.weboob.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}