简体   繁体   English

通过JavaScript添加固定位置时,粘性div切换侧面

[英]sticky div switches sides when fixed position added via JavaScript

I'm trying to implement a sticky div that is always present in the view when scrolling. 我试图实现滚动时始终在视图中存在的粘性div。 My javascript code works fine, and the div is indeed scrolling with the page. 我的JavaScript代码运行正常,并且div确实正在与页面滚动。 The problem is when I apply position: fixed; 问题是当我申请position: fixed; to the my sticky div of class scrolling-panel , it switches position from the right of the page (the original position) to the left. 到我的粘性scrolling-panel类的div,它将位置从页面的右侧(原始位置)切换到左侧。

Here is my HTML (most relevant part, still a lot of markup though) 这是我的HTML(最相关的部分,但仍然有很多标记)

<div class="container">
    <div class="main-container">
        <div class="row">
            <div class="col-md-12">
                <img id="brand-logo" src="images/logo.png">
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div id="container">
                    <img id='slider-img' src='images/img1.png'>
                    <div id='left_holder'><img onclick="slider(-1)" class='left' src="images/arrow_left.png"></div>
                    <div id='right_holder'><img onclick="slider(1)" class='right' src="images/arrow_right.png"></div>
                </div>
            </div>
        </div>
            <div class="scrolling-panel">
                <h3>Get Started</h3>
                <h3>$45</h3>
                <p>Join the Club for exclusive pric- ing. See a new box each month and skip if it’s not for you.</p>
                <button class="btn btn-success"><small>Join the club + get refresh</small></button>
                <p>Want Refresh without joining?
                        Quick Buy for $55.</p>
            </div>
        <div class="row">
            <div class="col-md-8">
                <div class="para-intro">
                    <h1>REFRESH</h1>
                    <p>Time to toss the cheap plastic bags -- your dopp kit should be
                        as composed as the rest of your travel getup. Our friends at
                        Men’s Journal helped us bring back this classic. Get ready to
                        breathe new life into your bathroom cabinet and carry-on.</p>
                </div>
            </div>
            <div class="col-md-8">
                <div class="sm-content-list">
                    <ul class="ul-content-list">
                    <li>DOPP KIT<br>
                        <span class='doppkit-value'>10"X6"X4"</span></li>
                    <li>DOUBLE HITTER<br>
                        2-IN-1 SHAMPOO &<br>
                        CONDITIONER<br>
                        <span class='doppkit-value'>8.5oz</span></li>
                    <li>CLEAN CUT<br>
                        SEMI-MATTE<br>
                        STYLING CREAM<br>
                        <span class='doppkit-value'>3.0oz</span></li>
                    <li>HANDMADE GOLD<br>
                        MOSS SOAP<br>
                        <span class='doppkit-value'>5.0oz</span></li>
                    <li>FACE WASH<br>
                        <span class='doppkit-value'>4.4oz</span></li>
                    <li>LOTION<br>
                        0.75oz</li>
                    <li>BODY POWDER<br>
                        <span class='doppkit-value'>1.0oz</span></li>
                    </ul>
                </div>
            <!-- <div class="col-md-5"> -->
                <div class="dopp-kit">
                    <h5>DOPP KIT, <span class='company-color'>BLUE CLAW CO.</span></h5>
                    <p>Blue Claw's Dopp bag is as resistant to blending in as it is to
                            wearing down. Designed exclusively for Bespoke, each bag
                            was painstakingly handcrafted in the good old US of A. The
                            waterproof exterior is crafted from heavy, 15oz waxed
                            canvas sourced from family mills in the heartland, while
                            the full-grain leather handle and pull tab, bronze rivets and
                            zipper, and signature blue ballistic nylon lining boast a
                            rugged simplicity. Americana at its best.</p>

                    <h5>DOPP KIT, <span class='company-color'>BLUE CLAW CO.</span></h5>
                    <p>Blue Claw's Dopp bag is as resistant to blending in as it is to
                            wearing down. Designed exclusively for Bespoke, each bag
                            was painstakingly handcrafted in the good old US of A. The
                            waterproof exterior is crafted from heavy, 15oz waxed
                            canvas sourced from family mills in the heartland, while
                            the full-grain leather handle and pull tab, bronze rivets and
                            zipper, and signature blue ballistic nylon lining boast a
                            rugged simplicity. Americana at its best.</p>

                    <h5>DOPP KIT, <span class='company-color'>BLUE CLAW CO.</span></h5>
                    <p>Blue Claw's Dopp bag is as resistant to blending in as it is to
                            wearing down. Designed exclusively for Bespoke, each bag
                            was painstakingly handcrafted in the good old US of A. The
                            waterproof exterior is crafted from heavy, 15oz waxed
                            canvas sourced from family mills in the heartland, while
                            the full-grain leather handle and pull tab, bronze rivets and
                            zipper, and signature blue ballistic nylon lining boast a
                            rugged simplicity. Americana at its best.</p>

                </div>
            </div>
                <div class="col-md-8">
                    <div class="tabbed-interface">
                        <div class="appearing-content">
                            <p>Blue Claw's Dopp bag is as resistant to blending in as it is to wearing down. Designed exclusively for Bespoke, each bag was painstakingly handcrafted in the good old US of A.</p>
                        </div>
                    </div>
                    <div class="clickable-links">
                            <a class='links-toggle'>blabla</a>
                            <a class='links-toggle'>blabla</a>
                            <a class='links-toggle'>blabla</a>
                    </div>
                    <div class="changing-para">
                        <p>Blue Claw's Dopp bag is as resistant to blending in as it is to wearing down. De- signed exclusively for Bespoke, each bag was painstakingly handcrafted in the good old US of A. The waterproof exterior is crafted from heavy, 15oz waxed canvas sourced from family mills in the heartland, while the full-grain leather handle and pull tab, bronze rivets and zipper, and signature blue ballistic nylon lining boast a rugged simplicity. Americana at its best.</p>
                    </div>
                </div>
        </div>
        <div class='media-objects'>
            <h3> From The Post </h3>
            <div class="row">
                <div class="col-md-3">
                    <div class="media">
                    <div class="media-left">
                    <img class="media-object" src="images/smallimage.png" alt="...">
                    </div>
                      <div class="media-body">
                            <br>
                            <p>The Perfect Side Part for any Hair Type</p>
                      </div>
                    </div>
                    <div class="media">
                    <div class="media-left">
                    <img class="media-object" src="images/smallimage.png" alt="...">
                    </div>
                      <div class="media-body">
                            <br>
                            <p>Grooming Tips</p>
                      </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="media">
                    <div class="media-left">
                    <img class="media-object" src="images/smallimage.png" alt="...">
                    </div>
                    <div class="media-body">
                        <br>
                        <p>How Soap is Made</p>
                    </div>
                    </div>
                    <div class="media">
                    <div class="media-left">
                    <img class="media-object" src="images/smallimage.png" alt="...">
                    </div>
                      <div class="media-body">
                            <br>
                            <p>Behind the Scenes with Sasquatch Soap</p>
                      </div>
                    </div>
                </div>
                <div class="col-md-3">
                    <div class="media">
                        <div class="media-left">
                        <img class="media-object" src="images/smallimage.png" alt="...">
                        </div>
                      <div class="media-body">
                        <br>
                            <p>Blue Claw's Adam Blitzer Talks Design</p>
                      </div>
                    </div>
                    <div class="media">
                        <div class="media-left">
                        <img class="media-object" src="images/smallimage.png" alt="...">
                        </div>
                      <div class="media-body">
                            <br>
                            <p>How To Shine Your Shoes</p>
                      </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

CSS (also using bootstrap for its grid system): CSS(也将引导程序用于其网格系统):

p {
    font-family: times;
    font-size: 15px;
}
#container {
    height: 400px;
    width: 650px;
    margin: 20px auto 35px auto;
    position: relative;
}
.main-container {
    margin: 40px 130px 40px 130px;
    border: solid black 1px;
}

#slider-img {
    height: 400px;
    width: 650px;
    position: absolute;
}
#left_holder {
    height: 400px;
    width: 100px;
    position: absolute;
    left: 0px;
    top: 0px;
}
#right_holder {
    height: 400px;
    width: 100px;
    position: absolute;
    right: 0px;
    top: 0px;

}
.left {
    height: 50px;
    width: 50px;
    position: absolute;
    top:40%;
    left: 0px;
}

.right {
    height: 50px;
    width: 50px;
    position: absolute;
    top:40%;
    right: 0px;
}

img#brand-logo {
    margin-top: 25px;
    margin-left: auto;
    margin-right: auto;
    display: block;
    height: 12px;
    width:120px;

}

.para-intro {
    margin-left: 110px;

}
.para-intro h1 {
    margin-top: 0px;
}
.sm-content-list {
 background-color: rgb(240,240,234);
 margin-left: 110px;
 float: left;
 margin-right: 10px;
 margin-bottom: 10px;
}

ul.ul-content-list {

    list-style-type: none;
    margin: 0px;
    padding: 5px 8px 5px 8px;
}
.ul-content-list li {
    font-size: 8px;
    font-weight: 600
}
.dopp-kit {
    margin-left: 110px;
}
.dopp-kit h5 {
    margin-bottom: 2px;
    margin-top: 0px;
}
.tabbed-interface {
    background-color: blue;
    margin-top: 20px;
    margin-left: 110px;
    height: 250px;
    width: 450px;
}
.clickable-links a {
    text-align: center;
    display: inline-block;
    margin-left: 110px;
    color: rgb(204,204,204);
    hover: none;
}
.clickable-links {
    text-align: center;
    margin-top: 10px;
}
.changing-para {
    margin-left: 110px;
    margin-bottom: 20px;
    margin-top: 30px;

}
.media-objects {
    margin-left: 110px;
    margin-bottom: 30px;
}
.media-objects h3 {
    margin-bottom: 30px;
}
.media-objects p {
    font-size: 12px;
}

.scrolling-panel {
    background-color: rgb(240,240,234);
    height: 270px;
    width: 170px;
    float: right;
    margin-right: 115px;
    text-align: center;
}


.scrolling-panel p {
    font-size: 10px;
}
.company-color {
    color: rgb(153,66,60);
}
.doppkit-value {
    color: rgb(151,151,151);
}
.appearing-content {
    color: white;
    float: bottom;
}
.stick {
        margin-right: 115px;
    position:fixed;
    top:0px;
}

Javascript for the sticky scrolling-panel div: 粘性scrolling-panel div的Javascript:

$(document).ready(function() {
    var s = $(".scrolling-panel");
    var pos = s.position();
    $(window).scroll(function() {
      var windowpos = $(window).scrollTop();
          if (windowpos >= pos.top) {
              s.addClass("stick");
          } else {
              s.removeClass("stick");
            }
    });
});

Let me know if I haven't been clear enough and/or if you need more information 让我知道我是否不够清楚和/或需要更多信息

Instead of margin-right on the element (in .stick class) use a right value. 代替元素(在.stick类中)的margin-right ,使用right值。

Here's a fiddle example 这是一个小提琴的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 position 从固定切换到 static 时,不会出现反向粘性导航栏? - Reverse sticky navbar not appearing when position switches from fixed to static? 在固定 position 中创建粘性 Div 的问题 - Issue with creating Sticky Div in fixed position 使用 javascript 和“位置:粘性”div 滚动 - Scrolling with javascript and 'position: sticky' div 通过 JavaScript 更新时固定位置 div 中的按钮消失(仅在 Firefox 移动浏览器中发生) - Button in fixed position div disappears when updated via JavaScript (only occurs in Firefox mobile browser) 粘性导航 - 固定 position 时宽度发生变化 - sticky nav - width changing when position fixed 如何在容器 div 中将 div 的 position 从固定更改为粘性? - how to change position of a div from fixed to sticky within a container div? 如何使粘性 div 响应浮动(位置:固定)div? - How to make sticky div respond to a floating (position: fixed) div? 不使用CSS位置的Javascript / jQuery粘性:固定 - Javascript / jQuery sticky without using css position: fixed 粘贴或固定覆盖到可滚动div-JavaScript的可见区域 - Sticky or fixed overlay to visible area of scrollable div- JavaScript 到达底部页脚时如何停止粘边栏的固定位置 - How to stop fixed position of sticky sidebar when bottom footer is reached
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM