简体   繁体   English

如何在javascript / jquery中单击文本时滚动到底部?

[英]How to scroll to bottom on click of text in javascript/jquery?

I am working on a website in which I want to scroll towards the bottom on click of a text in javascript/jquery. 我在一个网站上工作,我想在其中单击javascript / jquery中的文本时滚动到底部。

<p  class="ml-2 mb-2 d-flex  view_profile_options"><a href=""> view options</a> <i class="ml-2 fas fa-2x fa-angle-right"></i></p>

So on clicking of view options text above, it should take us to the Paris section in the fiddle . 因此,在单击上面的视图选项文本时,它应该带我们到小提琴中巴黎部分


Problem Statement: 问题陈述:

I am wondering what changes I should make in the fiddle so that on hitting view options text, it take us to the paris section tab with hover and content getting enabled automatically. 我想知道我应该在小提琴中进行哪些更改,以便在单击视图选项文本时将其带到具有悬停和自动启用 内容 功能的“ 巴黎”部分选项卡

I am pretty sure, I have to use the following jquery code but I am not sure how I can integrate in the current code.. 我很确定,我必须使用以下jquery代码,但是我不确定如何将其集成到当前代码中。

$("button").click(function() {
    $('html,body').animate({
        scrollTop: $(".second").offset().top},
        'slow');
});

Firstly, your jQuery is not binding the click behaviour to the correct a element. 首先,您的jQuery没有将点击行为绑定到正确a元素。 Consider the following updates: 请考虑以下更新:

$("p.view_profile_options a") // Causes the logic to attach to the link in 
.click(function(event) {      // your paragraph

    event.preventDefault(); // Causes the default browser behaviour for 
                            // link clicking to not happen

    $('html,body').animate({
        scrollTop: $(".tab").offset().top}, // Causes the scrolling to the
                                            // .tabs (ie as in your jsFiddle)
        'slow');
});

Updated answer 更新的答案

Here is the updated jQuery that includes the additional functionality for opening the Paris city tab, and activation of that tabs active style. 这是更新的jQuery,其中包括用于打开“巴黎城市”选项卡以及激活该选项卡active样式的附加功能。 An updated jsFiddle of this can be found here : 一个更新的jsFiddle可以在这里找到

/* Causes the logic to attach to the 
   link in your paragraph */
$("p.view_profile_options a") 
.click(function(event) {

  /* Causes the default browser behaviour 
     for link clicking to not happen */
  event.preventDefault(); 

  /* Open the 'paris' tab automatically */
  openCity(event, 'Paris')
  /* Cause the 'paris' tab to be 
     highlighted as active */
  $('.tab .tablinks:nth(1)').addClass('active')

    /* Causes the scrolling to the .tabs 
     (ie as in your jsFiddle) */
    $('html,body').animate({
        scrollTop: $(".tab").offset().top},
        'slow');
});

Here is the code that works jsfiddle . 这是jsfiddle的代码。 This is a function to scroll to bottom: 这是滚动到底部的功能:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    function scrollToBottom() {
        $('html, body').animate({scrollTop:$(document).height()}, 'slow');
    }
</script>

And all you need to do is call scrollToBottom() function from your link, so your entire <p> tag should look like this 您所需要做的就是从链接中调用scrollToBottom()函数,因此整个<p>标签应如下所示

 <p  class="ml-2 mb-2 d-flex  view_profile_options"><a href="javascript:scrollToBottom()"> view option</a> <i class="ml-2 fas fa-2x fa-angle-right"></i></p>

EDIT: I edited the jsfiddle to go to Paris section specifically. 编辑:我编辑了jsfiddle专门去巴黎部分。

On clicking view options it will take you the tab section. 单击视图选项后,将带您进入选项卡部分。

$(".view_profile_options a").click(function(event) {
    event.preventDefault();
    var sectionTop = $('.tab-contents').offset().top;
    $('html,body').animate({
        scrollTop: sectionTop },
        'slow');
 });

Fiddle Link : https://jsfiddle.net/moshiuramit/02znsurj/ 小提琴链接: https : //jsfiddle.net/moshiuramit/02znsurj/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM