简体   繁体   English

如何自动滚动相关的HTML表格

[英]How to auto-scroll a related html table

I have 2 tables, and they are related, because they are showing the same in different languages. 我有2个表,它们是相关的,因为它们以不同的语言显示相同的表。 When I press Key down or Key up , the item inside my clicked table moves and scrolls down, but not the related one. 当我按下Key downKey up ,单击的表中的项目会移动并向下滚动,但不会滚动相关的项目。 How can I make this? 我该怎么做?

Here is a piece of the html5 code: 这是一段html5代码:

<div id="first_table">
    <table id="first_header">
      <tr>
        <th class="field_1">1</th>                
        <th class="field_2">2</th>
        <th class="field_3">3</th>
        <th class="field_4">4</th>
        <th class="field_5">5</th>
      </tr>
    </table>
    <div id="first_table_container">
      <table id="first_text_table">
       <tbody id="first_text_table_body">
       </tbody>
      </table>
    </div>
    <div id="first_current">
     <textarea id="first_current_text" ></textarea>                           
     </div> 
</div>

<div id="second_table">
    <table id="second_header">
      <tr>
        <th class="field_1">1</th>                
        <th class="field_2">2</th>
        <th class="field_3">3</th>
        <th class="field_4">4</th>
        <th class="field_5">5</th>
      </tr>
    </table>
    <div id="second_table_container">
      <table id="second_text_table">
       <tbody id="second_text_table_body">
       </tbody>
      </table>
    </div>
    <div id="second_current">
     <textarea id="second_current_text" ></textarea>                           
     </div> 
</div>

Here the CSS: 这是CSS:

#first_table
{
width:90%;
float: left;
border-radius: 5px;
border-color: black;                                
border-collapse: collapse;    
margin-right:33px;
margin-bottom: 1%; 
}       
#second_table
{
width:90%;
float: right;
border-radius: 5px;
border-color: black;                                
border-collapse: collapse;    
margin-right:30px;
margin-bottom: 1%; 
}
#first_table_container, #second_table_container 
{
height: 200px;
overflow-y: scroll;
border-style: solid;
border-width: 0px;
border-color: #5e5e5e;                           
border-bottom-width:1px;
}

And now the javasrcipt (jQuery) code: 现在是javasrcipt(jQuery)代码:

if (e.keyCode == 38) { // up
                var index = $('#first_text_table_body tr > td').index();
                $('#second_text_table_body').scrollTo(index);
}

I've tried scrollTo() , but I can't make it works properly. 我已经尝试过scrollTo() ,但无法使其正常运行。 My goal is, if I press key up , my first list scrolls up and the second list has to scroll up as well. 我的目标是,如果按key up ,则第一个列表将向上滚动,第二个列表也必须向上滚动。

Thank you! 谢谢!

JSFiddle JSFiddle

HTML 的HTML

<div id="first_table">
    <table id="first_header">
        <tr>
            <th class="field_1">1</th>
            <th class="field_2">2</th>
            <th class="field_3">3</th>
            <th class="field_4">4</th>
            <th class="field_5">5</th>
        </tr>
    </table>
    <div id="first_table_container">
        <table id="first_text_table">
            <tbody id="first_text_table_body">
                <tr>
                    <td>abc</td>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                    <tr>
                        <td>abc</td>
                    </tr>
                </tr>
            </tbody>
        </table>
    </div>
    <div id="first_current">
        <textarea id="first_current_text"></textarea>
    </div>
</div>
<div id="second_table">
    <table id="second_header">
        <tr>
            <th class="field_1">1</th>
            <th class="field_2">2</th>
            <th class="field_3">3</th>
            <th class="field_4">4</th>
            <th class="field_5">5</th>
        </tr>
    </table>
    <div id="second_table_container">
        <table id="second_text_table">
            <tbody id="second_text_table_body"></tbody>
            <tr>
                <td>abc</td>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
                <tr>
                    <td>abc</td>
                </tr>
            </tr>
        </table>
    </div>
    <div id="second_current">
        <textarea id="second_current_text"></textarea>
    </div>
</div>

CSS 的CSS

#first_table {
    width:45%;
    float: left;
    border-radius: 5px;
    border-color: black;
    border-collapse: collapse;
    margin-right:33px;
    margin-bottom: 1%;
}
#second_table {
    width:45%;
    float: right;
    border-radius: 5px;
    border-color: black;
    border-collapse: collapse;
    margin-right:30px;
    margin-bottom: 1%;
}
#first_table_container, #second_table_container {
    height: 200px;
    overflow-y: scroll;
    border-style: solid;
    border-width: 0px;
    border-color: #5e5e5e;
    border-bottom-width:1px;
}

JS JS

$(document).ready(function () {
    $(document).keydown(function (e) {
        console.log(e);
        if (e.keyCode == 38) { //up
            $("#first_table_container").animate({
                scrollTop: 0
            });
            $("#second_table_container").animate({
                scrollTop: 0
            });
        }
        if (e.keyCode == 40) { //down
            $("#first_table_container").animate({
                scrollTop: 200
            });
            $("#second_table_container").animate({
                scrollTop: 200
            });
        }
    });
});

I used scrollTop instead, it just need a value of where to go. 我改用了scrollTop ,它只需要一个值即可。 my solution is just to get you starting. 我的解决方案只是让您入门。 you can continue this to calculate the scrollTop value you want to set on each click.. 您可以继续进行此操作,以计算每次单击时要设置的scrollTop值。

You should also clean the code. 您还应该清除代码。

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

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