根据按钮的点击移至特定的div [英] Move to specific div based on button click

查看:79
本文介绍了根据按钮的点击移至特定的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据上一个和下一个按钮移动div(这里是问题编号).这样所选的问题就始终在屏幕上可见.

I was trying to move the divs (here it's question number) based on the prev and next button. So that the selected question is always visible on screen.

这里是演示: http://jsfiddle.net/arunslb123/trxe4n3u/12/

屏幕:

单击并询问问题编号,然后单击上一个或下一个按钮以了解我的问题.

click and question number and click prev or next button to understand my issue.

我的代码:

$("#next")
.click(function () {
    $(".c.current-question")
        .each(function () {
            var divIdx = $(this)
                .attr('id');
            var scrollTo = $('#' + divIdx)
                .position()
                .left;
            $("#scrollquestion")
                .animate({
                    'scrollLeft': scrollTo
                }, 800);
        });
});

$("#prev")
.click(function () {
    $(".c.current-question")
        .each(function () {
            var divIdx = $(this)
                .attr('id');
            var scrollTo = $('#' + divIdx)
                .position()
                .left;
            $("#scrollquestion")
                .animate({
                    'scrollLeft': -scrollTo
                }, 800);
        });
});

推荐答案

使用scrollLeft有点棘手.我根据定位对您的用例做了一个小小的重做,然后根据容器的left对其进行了移动.棘手的部分是当滚动到最右端时要可靠地计算负位置.另外,需要考虑宽度和边距.

Using scrollLeft is a bit tricky. I did a small redo of your use-case based on positioning and then moving it based on left of the container. The tricky part is to reliably calculate the negative position when scrolled to the extreme right. Also, need to take into account the widths and margins.

检查以下代码段:

var $wrap = $("#numWrap"), $strip = $("#strip"), 
    $leftArrow = $(".wrapper > .arrows").first(), 
    wrapWidth = $wrap.width() + $leftArrow.width(), 
    margin = 10;
    
fill(20); select($(".numberItem").first());

$strip.on("click", ".numberItem", function() { select($(this)); });

function select($elem) {
    $(".numberItem").removeClass("selected");
    $elem.addClass("visited").addClass("selected");
    focus($elem[0]);    
}

function focus(elem) {
    var stripPos = $strip.position(), 
        numPos = $(elem).offset(), 
        elemWidth = $(elem).width() + margin, 
        numRight = numPos.left + elemWidth;
    
    if (numRight > wrapWidth) {
        $strip.css({"left": stripPos.left - elemWidth}); 
    }
    if (numPos.left < (margin + $leftArrow.width()))  {
        $strip.css({"left": stripPos.left + elemWidth}); 
    }
}

$(".wrapper").on("click", "a.arrow", function() {
    var stripPos = $strip.position();
    if (this.id == "lft") {
        $strip.css({"left": stripPos.left + (wrapWidth / 2)}); 
    } else {
        $strip.css({"left": stripPos.left - (wrapWidth / 2)});
    }
});

$(".controls").on("click", "a.arrow", function() {
    var $sel = $(".selected"), numPos, $sel, elemWidth; 
    	$elem = $sel.length > 0 ? $sel.first() : $(".numberItem").first();
    if (this.id == "lft") {
        $sel = $elem.prev().length > 0 ? $elem.prev() : $elem;
        select($sel); 
    } else {
        $sel = $elem.next().length > 0 ? $elem.next() : $elem;
        select($sel);
    }
    numPos = $sel.offset(); elemWidth = $sel.width() + margin;
    numRight = numPos.left + elemWidth;
    if (numPos.left > wrapWidth) {
        $strip.css({"left": -($sel.text()) * $sel.width() });
    }
    if (numRight < 0) {
        $strip.css({"left": +($sel.text()) * $sel.width() });
    }
});

function fill(num){
    for (var i = 1; i <= num; i++) {
        var $d = $("<a href='#' class='numberItem'>" + i + "</a>");
        $strip.append($d);
    }
}

* { box-sizing: border-box; padding: 0; margin: 0; font-family: sans-serif; }
div.wrapper { 
    background-color: #ddd; width: 100vw; height: 64px; 
    clear: both; overflow: hidden; margin-top: 16px;
}
div.arrows { 
    float: left; width: 10%; min-width: 24px; height: 64px; line-height: 64px;
    text-align: center; vertical-align: middle; overflow: hidden;
}
div.numWrap { 
    float: left; height: 64px; line-height: 64px;
    width: 80%; vertical-align: middle; 
    overflow: hidden; position: relative;
}
div.strip { 
    position: absolute; left: 0px;
    width: auto; white-space: nowrap; 
    transition: left 1s;
}
a.numberItem { 
    display: inline-block; text-align: center; margin: 0px 8px;
    background-color: #fff; border-radius: 50%; width: 48px; height: 48px; 
    font-size: 1.2em; line-height: 48px; text-decoration: none;
}
a.numberItem.visited { background-color: #fff; color: #000; border: 2px solid #01aebc; }
a.numberItem.selected { background-color: #01aebc; color: #fff; }
div.controls { clear: both; }
div.controls > div.arrows { width: auto; margin: 0 12px; }
a, a:focus, a:active, a:link, a:visited { 
    display: inline-block; 
    text-decoration: none; font-weight: 600; 
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
    <div class="arrows">
        <a id="lft" class="arrow" href="#">&#x3008;</a>
    </div>
    <div id="numWrap" class="numWrap">
        <div id="strip" class="strip"></div>
    </div>
    <div class="arrows">
        <a id="rgt" class="arrow" href="#">&#x3009;</a>
    </div>
</div>
<div class="controls">
    <div class="arrows">
        <a id="lft" class="arrow" href="#">&#x3008;&nbsp; Previous</a>
    </div>
    <div class="arrows">
        <a id="rgt" class="arrow" href="#">Next &nbsp;&#x3009;</a>
    </div>    
<div>

说明:

  1. 在数字容器上使用absolute定位,该容器嵌套以获取100%宽度.
  1. Using absolute positioning on the number container, which is nested to get 100% width.

标记:

<div class="wrapper">
    <div class="arrows"><a id="lft" class="arrow" href="#">&#x3008;</a></div>
    <div id="numWrap" class="numWrap">
        <div id="strip" class="strip"></div> <!-- nesting here -->
    </div>
    <div class="arrows"><a id="rgt" class="arrow" href="#">&#x3009;</a></div>
</div>

CSS:

div.wrapper { 
    background-color: #ddd; width: 100vw; height: 64px; 
    clear: both; overflow: hidden; margin-top: 16px;
}
div.arrows { 
    float: left; width: 10%; min-width: 24px; height: 64px; line-height: 64px;
    text-align: center; vertical-align: middle; overflow: hidden;
}
div.numWrap { 
    float: left; height: 64px; line-height: 64px;
    width: 80%; vertical-align: middle; 
    overflow: hidden; position: relative; /* relatively positioned */
}
div.strip { 
    position: absolute; left: 0px;        /* absolutely positioned */
    width: auto; white-space: nowrap; 
    transition: left 1s;                  /* instead of jquery animate */
}

有了这种结构,我们现在可以使用left来控制滚动了.

With this structure, we can now use left to control the scrolling.

  1. 对于部分被遮盖的数字,请尝试轻轻地聚焦(微移到视图中)一个部分被遮盖的数字.可以通过检查相对于父级的position并为其添加宽度/边距,并考虑左箭头的宽度(它可能会窥视)来实现.
  1. For partially obscured numbers, try to gently focus-in (nudge into view) a number which is partially obscured. This can be done by checking the position relative to parent and adding the width/margin to it and also accounting for width of the left arrow (it might peep thru).

JavaScript:

Javascript:

function focus(elem) {
    var stripPos = $strip.position(), 
        numPos = $(elem).offset(), 
        elemWidth = $(elem).width() + margin, 
        numRight = numPos.left + elemWidth;

    // if it is towards right side, nudge it back inside
    if (numRight > wrapWidth) {
        $strip.css({"left": stripPos.left - elemWidth}); 
    } 
    // if it is towards left side, nudge it back inside
    if (numPos.left < (margin + $leftArrow.width()))  {
        $strip.css({"left": stripPos.left + elemWidth}); 
    } 
}

  1. 一旦用户将列表滚动了太多,然后尝试单击上一个/下一个按钮来选择一个问题,那么我们需要将整个容器移到选定的编号.我们可以通过以下方式轻松地做到这一点:将问题编号乘以元素宽度,然后将left更改为正数(,如果向右)或将负数(如果是向左).
  1. Once the user has scrolled the list too far and then tries to click on previous / next buttons to select a question, then we need to move the entire container upto the selected number. We can easily do this by multiplying the question number with element width and then changing the left in positive (if towards right) or in negative (if towards left).

JavaScript:

Javascript:

// if left of element is more than the width of parent
if (numPos.left > wrapWidth) {
    $strip.css({"left": -($sel.text()) * $sel.width() });
}
// if right of element is less than 0 i.e. starting position
if (numRight < 0) {
    $strip.css({"left": +($sel.text()) * $sel.width() });
}


这是一个玩弄的小提琴: http://jsfiddle.net/abhitalks/aw166qhx/


Here is a fiddle to play with: http://jsfiddle.net/abhitalks/aw166qhx/

您将需要使其进一步适应您的用例,但您明白了.

You will need to further adapt it to your use-case, but you get the idea.

这篇关于根据按钮的点击移至特定的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆