最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: Cannot read property 'offsetParent' of null when navigatin to another page - Stack Overf

programmeradmin1浏览0评论

I'm using a code to stick a div on top when scrolling and the div reaches the top.

The code is working correctly but i'm getting a error when navigating to another page .

TypeError: Cannot read property 'offsetParent' of null

My code

render() {
  var startProductBarPos = -1;
  window.onscroll = function () {
    var bar = document.getElementById('nav');

    if (startProductBarPos < 0) startProductBarPos = findPosY(bar);

    if (window.pageYOffset > startProductBarPos) {
      bar.style.position = 'fixed';
      bar.style.width = '58.6%'
      bar.style.top = 0;
    } else {
      bar.style.position = 'relative';
      bar.style.width = '100%'
    }

  };

  function findPosY(obj) {
    var curtop = 0;
    if (typeof (obj.offsetParent) != 'undefined' && obj.offsetParent) {
      while (obj.offsetParent) {
        curtop += obj.offsetTop;
        obj = obj.offsetParent;
      }

      curtop += obj.offsetTop;
    } else if (obj.y) 
        curtop += obj.y;

    return curtop;
  };

  return(
    <div className="trait_type_header" id="nav">
    </div>
  );
}

This is line it's showing where the error is.

if (typeof (obj.offsetParent) != 'undefined' && obj.offsetParent) {

I think when navigating to another page obj bees null. If that's the case how can i fix it?

I'm using a code to stick a div on top when scrolling and the div reaches the top.

The code is working correctly but i'm getting a error when navigating to another page .

TypeError: Cannot read property 'offsetParent' of null

My code

render() {
  var startProductBarPos = -1;
  window.onscroll = function () {
    var bar = document.getElementById('nav');

    if (startProductBarPos < 0) startProductBarPos = findPosY(bar);

    if (window.pageYOffset > startProductBarPos) {
      bar.style.position = 'fixed';
      bar.style.width = '58.6%'
      bar.style.top = 0;
    } else {
      bar.style.position = 'relative';
      bar.style.width = '100%'
    }

  };

  function findPosY(obj) {
    var curtop = 0;
    if (typeof (obj.offsetParent) != 'undefined' && obj.offsetParent) {
      while (obj.offsetParent) {
        curtop += obj.offsetTop;
        obj = obj.offsetParent;
      }

      curtop += obj.offsetTop;
    } else if (obj.y) 
        curtop += obj.y;

    return curtop;
  };

  return(
    <div className="trait_type_header" id="nav">
    </div>
  );
}

This is line it's showing where the error is.

if (typeof (obj.offsetParent) != 'undefined' && obj.offsetParent) {

I think when navigating to another page obj bees null. If that's the case how can i fix it?

Share Improve this question edited Aug 8, 2018 at 13:20 Sasha Kos 2,6082 gold badges25 silver badges41 bronze badges asked Aug 8, 2018 at 11:36 CraZyDroiDCraZyDroiD 7,13733 gold badges108 silver badges196 bronze badges 3
  • If you track back through the code, that means your line var bar = document.getElementById('nav'); is failing to find the element. – Mitya Commented Aug 8, 2018 at 11:38
  • put your js after the html part – Edwin Commented Aug 8, 2018 at 11:41
  • this is reactjs. – CraZyDroiD Commented Aug 8, 2018 at 11:43
Add a ment  | 

1 Answer 1

Reset to default 5

Try to change

if (typeof (obj.offsetParent) != 'undefined' && obj.offsetParent)    

to

if (obj && obj.offsetParent)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论