简体   繁体   English

scrollTop 按钮仅适用于第一次点击

[英]scrollTop button works on the first click only

I have a div that uses overflow-y: scroll;我有一个使用overflow-y: scroll;的 div overflow-y: scroll; I want to use a button that on click it will move to the next row on the div.我想使用一个按钮,点击它会移动到 div 上的下一行。 My code works perfect for the first click only after that no action for the click.我的代码只适用于第一次点击,然后没有点击操作。

 <button class="big-scrolTopBtn" id="slideLeft" type="button"><span class="fa fa-chevron-up"></span></button>
        <div class="partners-inner big-scrol" id="scrol">
            <div class="">

                <div class="row media-row-height " style="text-align:center;" dir="rtl">
                    @foreach (var item in Model)
                    {
                       
                            foreach (var item1 in item.tbl_Videos)
                            {

                                <div class="col-md-6 col-lg-3 col-sm-6 col-xs-12 left-border directors-top-padding" style="text-align:center;">
                                    @if (!string.IsNullOrEmpty(item1.PhotoURL))
                                    { 
                                                <a href="@item1.VideoURL">
                                                </a>
                                                <img src="@Url.Content(String.Format(ConfigurationManager.AppSettings["AdminDomain"] + "{0}", Url.Content(item1.PhotoURL)))" class="img-media">

                                    }
                                </div>

                            }
                        }
                    </div>

            </div>
        </div>
        <button class="scrolBotBtn" id="slideRight" type="button"><span class="fa fa-chevron-down"></span></button>

JS: JS:

$(document).ready(function () {

        $('#slideRight').click(function () {

            $('#scrol').scrollTop(300);
        });

        $('#slideLeft').click(function () {
            $('#scrol').scrollTop(-300);
        });
    });

CSS: CSS:

.big-scrol {
        height: 300px;
        overflow-y: scroll;
    }

    .media-row-height {
        height: 900px;
        width: 1500px;
        margin: auto;
        padding: 10px;
    }

For it to work correctly, you must add or subtract value with each click.为了使其正常工作,您必须在每次单击时添加或减去值。

We also need to know how high the scrolling element is to be able to stop adding values.我们还需要知道滚动元素有多高才能停止添加值。 For this I added the ID id="wrap" of the element being scrolled为此,我添加了正在滚动的元素的 ID id="wrap"

 $(document).ready(function () { var top = 0; var winH = $('#wrap').height(); $('#slideRight').click(function () { if (top < winH) { top = top + 300; }; $('#scrol').scrollTop(top); }); $('#slideLeft').click(function () { top = top - 300; if (top < 0) { top = 0 }; $('#scrol').scrollTop(top); }); });
 .big-scrol { height: 300px; overflow-y: scroll; } .media-row-height { height: 900px; width: 1500px; margin: auto; padding: 10px; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <button class="big-scrolTopBtn" id="slideLeft" type="button"><span class="fa fa-chevron-up">UP</span></button> <div class="partners-inner big-scrol" id="scrol"> <div class=""> <div id="wrap" class="row media-row-height" style="text-align:center;" dir="rtl"> <!-- @foreach (var item in Model) { foreach (var item1 in item.tbl_Videos) { <div class="col-md-6 col-lg-3 col-sm-6 col-xs-12 left-border directors-top-padding" style="text-align:center;"> @if (!string.IsNullOrEmpty(item1.PhotoURL)) { <a href="@item1.VideoURL"> </a> <img src="@Url.Content(String.Format(ConfigurationManager.AppSettings[" AdminDomain"] + "{0}" , Url.Content(item1.PhotoURL)))" class="img-media"> } </div> } } --> </div> </div> </div> <button class="scrolBotBtn" id="slideRight" type="button"><span class="fa fa-chevron-down">DOWN</span></button>

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

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