들레아빠

상단으로 를 상하 이동 및 퍼센트 표시 하기

· 2023-07-17 (월) 10:09:49 · 1745 · 6
사용자 지정 3.png

아래 내용으로 lib(테마/lib)에 button_top.php파일(첨부파일)을 만들어 넣으세요.

[code]

<button type="button" id="button_top">
  <i class="fa fa-arrows-v" aria-hidden="true"></i><span class="sound_only">상단으로</span>
</button>

<script>
jQuery(function($) {
  var $elem = $("html, body");
  var where = 0;
  $("#button_top").on("click", function() {
    var windowHeight = $(document).height() - $(window).height();
    var winhalf = windowHeight * 0.5; // Calculate the new winhalf value

    if ($elem.scrollTop() < winhalf) {
      $elem.animate({ scrollTop: $elem.prop("scrollHeight") }, 400);
      where = 1;
    } else {
      $elem.animate({ scrollTop: 0 }, 400);
      where = 0;
    }
  });
});

$(document).ready(function() {
  $(window).scroll(function() {
    var windowHeight = $(document).height() - $(window).height();
    var winhalf = windowHeight * 0.5; // Calculate the new winhalf value
    var scrollPos = $(document).scrollTop();
    var scrollPercent = (scrollPos / windowHeight) * 100;
    var $buttonTop = $('#button_top'); // Selector variable

    if (scrollPos > winhalf) {
      $buttonTop.css({
        "border-color": "red",
        "color": "red"
      }).html("<i class='fa fa-arrow-up' aria-hidden='true'></i>");
    } else {
      $buttonTop.css({
        "border-color": "blue",
        "color": "blue"
      }).html("<i class='fa fa-arrow-down' aria-hidden='true'></i>");
    }

    // 퍼센트 출력 시작.
    if ($('#scroll_percentage').length === 0)
        {
            $('<p id="scroll_percentage"></p>').appendTo($buttonTop);
        }
        $('#scroll_percentage').text(Math.round(scrollPercent));
    // 퍼센트 출력 끝.
    // 다크모드 시작.
    var xhr = new XMLHttpRequest();
    xhr.open('HEAD', '<?php echo G5_THEME_CSS_URL?>/default_dark.css', false);
    xhr.send();

    if (xhr.status == "200") {
        if (scrollPos > 100 && scrollPos < 200) {
          $('#button_top').css("display", "none");
          $('.fix_ch').css("display", "block");
        } else {
          $('.fix_ch').css("display", "none");
          $('#button_top').css("display", "block");
        }
    } else {
    //    alert(xhr.status + "파일이 없습니다.");
    }
    // 다크모드 끝.
    // 메뉴 상단고정 시작.
    if (!g5_is_mobile) {
        $('#gnb').css({
          "display": "block",
          "z-index": "10000"
        });
        if (scrollPos > 186) {
          $('#gnb').css({
            "position": "fixed",
            "top": "0",
            "width": "100%"
          });
        } else {
          $('#gnb').css({
            "position": "relative"
          });
        }
    }
    // 메뉴 상단고정 끝.
  });
});
</script>

<style>
  #top_btn, .fix_ch {
    display:none
  }

  #button_top {
    position: fixed;
    bottom: 20px;
    right: 3px;
    width: 43px;
    height: 43px;
    line-height: 40px;
    border-radius: 50%;
    border: 1px solid #33333310;
    color: #333;
    text-align: center;
    font-size: 15px;
    z-index: 220;
    background: rgba(255, 255, 255, 0.5);
  }

  #button_top:hover {
    border-color: #3059c750;
    background: #3059c750;
    color: #333;
  }

  #scroll_percentage {
    font-size: 12px;
    margin-top: -27px;
    font-weight: bold;
    z-index: 999;
  }
</style>

[/code]

tail.sub.php 상단 ?>바로 위에 아래내용 추가

[code]$config['cf_theme'] ? include_once(G5_THEME_PATH.'/lib/button_top.php') : include_once(G5_PATH.'/lib/button_top.php');[/code]

첨부파일

사용자 지정 3.png (3.6 KB)
2회 2023-07-17 10:17
(0 bytes) 0회 2023-07-17 10:17
button_top.zip (1.4 KB) 4회 2023-07-17 10:09
|

댓글 6개

멋진 팁 고맙습니다.
@rainbi 네 감사 합니다. 기능 설명이 더 필요한데 생략 되었습니다.
공유 감사드립니다. 저 같은 초보들에게 큰 힘이 됩니다 ^^
감사 합니다. 스크롤 하면 상단 메뉴 고정됩니다.
아주 좋아요. 감사합니다.

1. /lib 폴더에 넣은 경우
include_once(G5_PATH.'/lib/button_top.php');

2. /theme/사용테마/lib 폴더에 넣은 경우
include_once(G5_THEME_PATH.'/lib/button_top.php')
sinbi님 감사 합니다.
댓글을 작성하시려면 로그인이 필요합니다.

개발자팁

5,403건

개발과 관련된 유용한 정보를 공유하세요. 질문은 QA에서 해주시기 바랍니다.

+
분류 제목 글쓴이 날짜 조회
PHP 23-07-20 조회 2,140
기타 23-07-20 조회 1,475
PHP 23-07-20 조회 1,596
23-06-23 조회 1,736
23-07-04 조회 1,683
23-07-19 조회 1,359
23-07-17 조회 1,746
23-07-15 조회 1,672
23-07-07 조회 2,146
23-07-07 조회 1,661
PHP 23-07-19 조회 1,349
PHP 23-07-18 조회 2,765
PHP 23-07-17 조회 2,563
PHP 23-07-16 조회 3,294
PHP 23-07-15 조회 1,602
JavaScript 23-07-15 조회 1,699
JavaScript 23-07-14 조회 1,538
JavaScript 23-07-13 조회 1,580
PHP 23-07-13 조회 1,646
기타 23-07-12 조회 1,500
JavaScript 23-07-12 조회 1,522
JavaScript 23-07-11 조회 1,555
JavaScript 23-07-10 조회 1,492
JavaScript 23-07-09 조회 1,739
JavaScript 23-07-08 조회 1,349
기타 23-07-08 조회 1,636
PHP 23-07-07 조회 1,575
JavaScript 23-07-06 조회 1,320
PHP 23-07-05 조회 3,077
PHP 23-07-04 조회 1,493