xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

css how to realize fixed element relative to parent element position

css how to realize fixed element relative to parent element position

css fixed 相对父元素定位

absolute bug ❌ / fixed bug ❌

absolute 滚动 bug / fixed 滚动 bug

添加 transform: translate(0, 0); 使 fixed 相对父元素定位

.assemble-creative-toggle-card-box {
    box-sizing: border-box;
    position: relative;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    flex-flow: row wrap;
    // width: calc(325px + 16px);
    width: 325px;
    height: 185px;
    overflow-y: scroll;
    // overflow-y: auto;
    background: #F7F7F7;
    border: 1px solid #EEEEEE;
    padding: 10px;
    // fixed 参考
    // transform: translateY(0);
    // z-index: 1;
    // transform: translate(0, 0);
    /* hidden scrollbar */
    &::-webkit-scrollbar {
        display: none;
        width: 0 !important;
        height: 0 !important;
        background: transparent;
    }
    .creative-toggle-card-header {
        // margin-top: 10px;
        // position: fixed;
        // position: absolute;
        position: sticky;
        height: 0;
        transform: translate(-10px, -10px);
        top: 0;
        right: 0;
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        width: 100%;
        z-index: 2;
    }
}

sticky bug ❌

sticky 不滚动时候,占用高度空间 bug ❌


solution ✅ 🚀

.awesome-fixed-solution {
        position: sticky;
        height: 0;
        // ✅ height 设置为 0, 防止 sticky 没有滚动的时候占用空间,保持与滚动的时候一致性!
        transform: translate(-10px, -10px);
        // ✅调整偏移量,等价于本来 absolute 的 top, right 的负值
}

<style lang="scss">
.assemble-creative-toggle-card-box {
    box-sizing: border-box;
    position: relative;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    flex-flow: row wrap;
    // width: calc(325px + 16px);
    width: 325px;
    height: 185px;
    overflow-y: scroll;
    // overflow-y: auto;
    background: #F7F7F7;
    border: 1px solid #EEEEEE;
    padding: 10px;
    // fixed 参考
    // transform: translateY(0);
    // z-index: 1;
    // transform: translate(0, 0);
    /* hidden scrollbar */
    &::-webkit-scrollbar {
        display: none;
        width: 0 !important;
        height: 0 !important;
        background: transparent;
    }
    .creative-toggle-card-header {
        // margin-top: 10px;
        // position: fixed;
        // position: absolute;
        position: sticky;
        height: 0;
        transform: translate(-10px, -10px);
        top: 0;
        right: 0;
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        width: 100%;
        z-index: 2;
        .toggle-switch {
            margin-top: 2px;
            margin-left: 4px;
            width: 14px;
            height: 14px;
            border-radius: 2px;
        }
        .tags-box {
            display: flex;
            align-items: flex-start;
            justify-content: flex-end;
            margin-top: 4px;
            margin-right: 4px;
            .recommended-box {
                height: 20px;
                line-height: 20px;
                // font-size: 12px;
                color: #FFFFFF;
                margin-right: 4px;
                .recommended-tag {
                    padding: 2px 6px;
                    display: inline-block;
                    height: 17px;
                    line-height: 17px;
                    background: #E6A23C;
                    border-radius: 2px;
                    text-align: center;
                    font-size: 12px;
                }
                .unrecommended-tag {
                    padding: 2px 6px;
                    display: inline-block;
                    height: 17px;
                    line-height: 17px;
                    background: #999999;
                    border-radius: 2px;
                    text-align: center;
                    font-size: 12px;
                }
            }
            .score-box {
                margin-right: -20px;
                // ✅ 右侧 item 对齐偏移量
                // box-sizing: border-box;
                display: flex;
                align-items: center;
                justify-content: center;
                height: 20px;
                line-height: 20px;
                background: #FCF5EB;
                border-radius: 2px;
                border: 1px solid #F7E3C4;
                .score-text {
                    display: inline-block;
                    height: 17px;
                    line-height: 17px;
                    font-size: 12px;
                    color: #E6A23C;
                    // width: 24px;
                    text-align: center;
                    padding: 2px 4px;
                    font-size: 12px;
                }
                .separate-line {
                    box-sizing: border-box;
                    display: inline-block;
                    width: 1px;
                    height: 10px;
                    background: #F7E3C4;
                    border-radius: 1px;
                    padding: 0;
                    margin: 0;
                }
            }
        }
    }
    .creative-toggle-card-body {
        box-sizing: border-box;
        // width: 150px;
        width: 147px;
        height: 80px;
        background: rgba(0, 0, 0, 0.5);
        border-radius: 4px;
        // height: 100%;
        max-height: 80px;
        overflow-y: hidden;
        margin-bottom: 5px;
        &:nth-of-type(2n) {
            margin-right: 5px;
        }
        &:nth-last-of-type(1) {
            margin-bottom: 0;
        }
        &:nth-last-of-type(2) {
            margin-bottom: 0;
        }
        .img-cover-url {
            box-sizing: border-box;
            width: 100%;
            // height: auto;
            height: 100%;
        }
        .assemble-video {
            position: relative;
            .tt-video-player {
                position: absolute;
                left: 50%;
                top: 50%;
                box-sizing: border-box;
                width: 32px;
                height: 32px;
                transform: translateX(-50%) translateY(-50%);
            }
        }
    }
    .creative-toggle-card-fotter {
        //
    }
}
</style>


MDN position




refs

https://developer.mozilla.org/en-US/docs/Web/CSS/position



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(74)  评论(4编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-07-05 web components & publish custom element & npm
2020-07-05 git cli All In One
2020-07-05 UI Design & App & Free Icons All In One
2020-07-05 Flutter & release an iOS app
2020-07-05 codesign wants to access key 密码是什么
2019-07-05 flex & flex-wrap
2016-07-05 Web Accessibility Initiative (WAI) ===Corporate Social Responsibility(CSR)===Search Engine Optimization(SEO)===Accessible Rich Internet Applications (ARIA)
点击右上角即可分享
微信分享提示