js 공백 관련 질문입니다.

js 공백 관련 질문입니다.

QA

js 공백 관련 질문입니다.

본문

<div id="section-four"  title="bestitem"  ></div>

<div id="section-five"  title="new arrivals"  ></div>

쇼핑몰에서 클릭시 해당 섹션으로 이동하고 스크롤시 해당 섹션 타이틀이 활성화 되는 소스입니다.

다른 부분은 정상 작동하는데 위 소스에서 title 부분 문자에 공백을 넣으면 해당 부분이 활성화 되지 않는 문제가 있네요.

 

스크립에서 공백처리를 해줘야 할  것 같은데요 구글링 6시간 하다가 도저히 안되어서 회원님들의 도움 구해봅니다.

 

아래가 스크립트 소스입니다.

------------------------------------

 

 jQuery(function($) {

  var html = $('html');
  var viewport = $(window);
  var viewportHeight = viewport.height();

  var scrollMenu = $('#section-menu');
  var timeout = null;

  function menuFreeze() {
    if (timeout !== null) {
      scrollMenu.removeClass('freeze');
      clearTimeout(timeout);
    }

    timeout = setTimeout(function() {
      scrollMenu.addClass('freeze');
    }, 2000);
  }
  scrollMenu.mouseover(menuFreeze);

  /* ==========================================================================
       Build the Scroll Menu based on Sections .scroll-item
       ========================================================================== */

  var sectionsHeight = {},
    viewportheight, i = 0;
  var scrollItem = $('.scroll-item');
  var bannerHeight;

  function sectionListen() {
    viewportHeight = viewport.height();
    bannerHeight = (viewportHeight);
    $('.section').addClass('resize');
    scrollItem.each(function() {
      sectionsHeight[this.title] = $(this).offset().top;
    });
  }
  sectionListen();
  viewport.resize(sectionListen);
  viewport.bind('orientationchange', function() {
    sectionListen();
  });

  var count = 0;

  scrollItem.each(function() {
    var anchor = $(this).attr('id');
    var title = $(this).attr('title');
    count++;
    $('#section-menu ul').append('<li><a id="nav_' + title + '" href="#' + anchor + '"><span>' + count + '</span> ' + title + '</a></li>');
  });

  function menuListen() {
    var pos = $(this).scrollTop();
    pos = pos + viewportHeight * 1.75;
    for (i in sectionsHeight) {
      if (sectionsHeight[i] < pos) {
        $('#section-menu a').removeClass('active');
        $('#section-menu a#nav_' + i).addClass('active');;
        var newHash = '#' + $('.scroll-item[title="' + i + '"]').attr('id');
        if (history.pushState) {
          history.pushState(null, null, newHash);
        } else {
          location.hash = newHash;
        }
      } else {
        $('#section-menu a#nav_' + i).removeClass('active');
        if (pos < viewportHeight - 72) {
          history.pushState(null, null, ' ');
        }
      }
    }
  }
  scrollMenu.css('margin-top', scrollMenu.height() / 2 * -1);

  /* ==========================================================================
       Smooth Scroll for Anchor Links and URL refresh
       ========================================================================== */

  scrollMenu.find('a').click(function() {
    var href = $.attr(this, 'href');
    $('html').animate({
      scrollTop: $(href).offset().top
    }, 500, function() {
      window.location.hash = href;
    });
    return false;
  });

  /* ==========================================================================
       Fire functions on Scroll Event
       ========================================================================== */

  function scrollHandler() {
    menuListen();
    menuFreeze();
  }
  scrollHandler();
  viewport.on('scroll', function() {
    scrollHandler();
    //            window.requestAnimationFrame(scrollHandler);
  });
});

#js

이 질문에 댓글 쓰기 :

답변 1

var newHash = '#' + $('.scroll-item[title="' + i + '"]').attr('id');

해당 위치를 찾는데 공백을 넣으면 위치를 찾는 부분을 못찾아서 그렇습니다.

 

타이틀 이후에 다른걸 넣고 싶으시면 타이틀 다음이 아니라

id 다음에 넣으세요

답변을 작성하시기 전에 로그인 해주세요.
전체 308
QA 내용 검색
filter #js ×

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT