เปลี่ยนฟังก์ชัน scrollTo เพื่อให้รูปภาพถัดไป/ก่อนหน้ามาอยู่ตรงกลางแทนที่จะเป็นด้านซ้าย

ฉันมี div แบบเลื่อนแนวนอนที่มีรูปภาพ

เมื่อใช้ jQuery ฉันมีมันเพื่อให้การคลิกครึ่งขวาของ div จะเลื่อนไปที่รูปภาพถัดไป และครึ่งซ้ายจะเลื่อนไปที่รูปภาพก่อนหน้า ในปัจจุบัน หลังจากฟังก์ชัน scrollTo รูปภาพถัดไป/ก่อนหน้าจะมาทางด้านซ้ายของคอนเทนเนอร์

ฉันจะเปลี่ยนให้มาอยู่กึ่งกลางคอนเทนเนอร์ได้อย่างไร และเปลี่ยนฟังก์ชัน currentElement เพื่อตรวจสอบว่ารูปภาพอยู่กึ่งกลางเมื่อใด

$(document).ready(function(){
var container = $("#sg-scroll"),
    child = $(".sg-pic_wrap"),
    current;

    // CURRENT ELEMENT CHECK
function currentElem () {
    child.each(function(){

        var x = $(this).offset().left,
            w = $(this).width();

        if (x <= 0 && x < w ) {
            current = $(this);
        }
    })
}

// MOVE TO ELEM
function scrollTo (elem) {
    container.scrollLeft(elem);
}


// CLICK
container.on("click", function(e){
    var _w = $(window).width();
    if ((e.pageX) <= (_w/2)) { // LEFT
        currentElem();
            if (current.prev().length > 0) { // otherwise we get undefined error
                scrollTo( parseInt((container.scrollLeft()) + (current.prev().offset().left) + 5 ) );
            }
    }
    else { // RIGHT
            currentElem();
            scrollTo( parseInt((container.scrollLeft()) + (current.next().offset().left) + 5 ) );
    }
});


// CURSOR ...
container.on("mousemove", function(e){
    var _w = $(window).width();
    if ((e.pageX) <= (_w/2)) { // LEFT
    }
    else { // RIGHT
    }
});

});//document.ready

นี่คือ Fiddle สำหรับใช้ในแกลเลอรี


person Joe    schedule 13.01.2016    source แหล่งที่มา
comment
jsfiddle.net/3zLg4p8v/7 ฉันได้ทำการเปลี่ยนแปลงบางอย่างและทำงานได้ดี อาจมีจุดบกพร่องที่ต้องแก้ไขเพิ่มเติม เลยไม่ได้โพสต์เป็นคำตอบ   -  person tkay    schedule 13.01.2016
comment
ขอบคุณสำหรับการตอบกลับ. ใช่แล้ว scrollTo ทำงานได้ดี มันทำให้อยู่ตรงกลาง ดูเหมือนว่าจะมีปัญหากับสิ่งที่คิดว่าเป็นองค์ประกอบปัจจุบันเท่านั้นหากคุณเลื่อนด้วยตนเอง มีเพียงปุ่มถัดไปเท่านั้น   -  person Joe    schedule 14.01.2016


คำตอบ (1)


ตรวจสอบความคิดเห็น // CHANGED ของฉันใน JSFiddle

$(document).ready(function() {
    // cache here reusable elements
    var container = $("#sg-scroll"),
        child = $(".sg-pic_wrap"),
        current;

    // CURRENT ELEMENT CHECK
    function currentElem() {
        // .each() instead of for(i = 0; i < var;i++)
        child.each(function() {

            // these variables must  be updated each time
            // we launch the function, so we cache them inside
            var x = $(this).offset().left,
                w = $(this).width(),
                cW = $('#sg-container').width() / 2; // CHANGED

            if (x < cW && x + w > cW) { // CHANGED
                current = $(this);
            }



        })
    }

    // MOVE TO ELEM
    function scrollTo(elem, width) {
        elem = elem + (width/2) - ($('#sg-container').width()/2); // CHANGED
        container.scrollLeft(elem);
    }


    // CLICK
    container.on("click", function(e) {
        var _w = $(window).width();
        if ((e.pageX) <= (_w / 2)) { // LEFT
            // we only need to check the current element each time we click, not on scroll
            // doing this will result in faster js
            currentElem();
            if (current.prev().length > 0) { // otherwise we get undefined error
                //scrollTo( parseInt((container.scrollLeft()) + (current.prev().offset().left) + 5) );
                scrollTo(container.scrollLeft() + current.prev().position().left, current.prev().width()); // CHANGED
            }
        } else { // RIGHT
            currentElem();
            //scrollTo( parseInt((container.scrollLeft()) + (current.next().offset().left) + 5 ) );
            scrollTo(container.scrollLeft() + current.next().position().left, current.next().width()); // CHANGED
        }
    });


    // CURSOR ...
    container.on("mousemove", function(e) {
        var _w = $(window).width();
        if ((e.pageX) <= (_w / 2)) { // LEFT
            //change cursor here
        } else { // RIGHT
            // or here
        }
    });



}); //document.ready
person Baro    schedule 13.01.2016
comment
ขอบคุณ มันใช้งานได้ดี! มีวิธีเพิ่ม .animate เข้าไปอย่างง่ายดายเพื่อให้สกรอลล์เป็นภาพเคลื่อนไหว แทนที่จะกระโดดไปที่รูปภาพหรือไม่ - person Joe; 14.01.2016
comment
@Joe ใช่ ตรวจสอบสิ่งนี้: jsfiddle.net/3zLg4p8v/9 บรรทัดเดียวที่อัปเดตมีความคิดเห็น '// ปรับปรุง'. :) - person Baro; 14.01.2016