te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>css - Sticky sidebar javascript without jQuery - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - Sticky sidebar javascript without jQuery - Stack Overflow

programmeradmin4浏览0评论

How do you make a sticky vertical sidebar with stopper, but without jQuery? Are there any snippets/plugins? I don't need it to support older browsers.

I don't mean just position: fixed, it must stay in the same place and then start being sticky (fixed) when you've scrolled past a certain point. It must then stop following at the stop point.

Like , but NOT jQuery. There are many jQuery plugins available.

How do you make a sticky vertical sidebar with stopper, but without jQuery? Are there any snippets/plugins? I don't need it to support older browsers.

I don't mean just position: fixed, it must stay in the same place and then start being sticky (fixed) when you've scrolled past a certain point. It must then stop following at the stop point.

Like http://stickyjs., but NOT jQuery. There are many jQuery plugins available.

Share Improve this question edited Jun 16, 2013 at 17:19 Opptatt Jobber asked Jun 16, 2013 at 17:01 Opptatt JobberOpptatt Jobber 3195 silver badges13 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 7

Basically it's as easy as this:

window.onscroll = function() {
    var sticky = document.getElementById('stickynav');
    if( document.body.scrollTop+document.documentElement.scrollTop > 240)
        sticky.className = "stuck";
    else sticky.className = "";
};

Then just define styles in the .stuck class that add things like position:fixed to the element.

Here's an attempt that doesn't require position: fixed changes, uses the element-document distance as reference and let's you scroll to the bottom:

var node = document.getElementById('side'),  // your element
    nodeOffs = node.offsetTop,               // distance relative to its parent
    parent = node;

// loop through parents to determine the distance relative to the document top
while(parent = parent.offsetParent)
  if(parent.offsetTop)
    nodeOffs += parent.offsetTop;

window.addEventListener('scroll', function(event){    

  // current scroll position relative to the body
  var scrollPos = document.body.scrollTop;

  if(scrollPos > nodeOffs){

    // don't do anything if the elements height is larger than the
    // remaining scroll content height (distance including)
    if(scrollPos < (document.body.scrollHeight - node.clientHeight - nodeOffs))
      node.style.top = (scrollPos - nodeOffs) + 'px';

  }else{
      node.style.top = '0px';
  }

});    

It's likely that this won't handle all possible cases (it doesn't take into account margins for example). That's why there's a plugin that you should use it if you don't want to tweak the js yourself....

(test)

It is possible to do it with shorter JS.

const stickyDiv = document.querySelector(".sticky");
window.addEventListener("scroll", function() {
  stickyDiv.style.top = window.pageYOffset + "px";
});
.left {
  position: relative;
}
.sticky {
  position: absolute;
}

  /* below is unecessary CSS just to demo the page */
.left {
  width:40%;
  float:left;
}
.right {
  width: 40%;
  min-height: 3000px;
  float:right;
}
<div class="container">
  <div class="row">
    
    <div class="left">
      <article class="sticky">
        <h2>Sticky sidebar title here</h2>
        <p>Text here</p>
      </article>
    </div>
    
    <div class="right">
      <article>
        <h2>Regular page here</h2>
        <img src="http://lorempixel./400/200/cats">
        <p>Regular page content here.</p>
      </article>
    </div>
    
  </div>
</div>

https://codepen.io/MistaPrime/pen/vYYxvza

I know this is an old question and that it is asking for a JavaScript solution BUT, as the main idea here is to get a sticky sidebar working, I have to share that there is a pure CSS solution that works just as expected:

.sticky {
    position: -webkit-sticky; /* Safari */
    position: sticky;
    top: 0;
}

Just apply that sticky class to your sidebar elements and that will work.

That will, for sure, save you all a lot of time and effort.

REF: https://developer.mozilla/es/docs/Web/CSS/position#sticky

发布评论

评论列表(0)

  1. 暂无评论