ฉันกำลังพยายามย้ายหน้าไปยัง<div>
องค์ประกอบ
ฉันได้ลองใช้รหัสถัดไปแล้วเพื่อไม่เกิดประโยชน์:
document.getElementById("divFirst").style.visibility = 'visible';
document.getElementById("divFirst").style.display = 'block';
ฉันกำลังพยายามย้ายหน้าไปยัง<div>
องค์ประกอบ
ฉันได้ลองใช้รหัสถัดไปแล้วเพื่อไม่เกิดประโยชน์:
document.getElementById("divFirst").style.visibility = 'visible';
document.getElementById("divFirst").style.display = 'block';
คำตอบ:
คุณสามารถใช้จุดยึดเพื่อ "โฟกัส" div เช่น:
<div id="myDiv"></div>
จากนั้นใช้จาวาสคริปต์ต่อไปนี้:
// the next line is required to work around a bug in WebKit (Chrome / Safari)
location.href = "#";
location.href = "#myDiv";
location.href="#";location.href="#myDiv"
การใช้งาน: การใช้งานid="myDiv"
เป็นที่ต้องการมากกว่าname="myDiv"
และทำงานเกินไป
scrollIntoView ทำงานได้ดี:
document.getElementById("divFirst").scrollIntoView();
เอกสารอ้างอิงฉบับเต็มใน MDN:
https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView
scrollIntoView
คำถามและคำตอบของคุณดูแตกต่าง ฉันไม่รู้ว่าฉันเข้าใจผิด แต่สำหรับผู้ที่ googles และไปถึงที่นี่คำตอบของฉันจะเป็นดังต่อไปนี้:
คำตอบของฉันอธิบาย:
นี่คือจาวาสคริปต์ง่ายๆสำหรับสิ่งนั้น
เรียกสิ่งนี้เมื่อคุณต้องการเลื่อนหน้าจอไปยังองค์ประกอบที่มี id = "yourSpecificElementId"
window.scroll(0,findPos(document.getElementById("yourSpecificElementId")));
กล่าวคือ สำหรับคำถามข้างต้นหากเจตนาคือการเลื่อนหน้าจอไปยัง div ด้วย id 'divFirst'
รหัสจะเป็น: window.scroll(0,findPos(document.getElementById("divFirst")));
และคุณต้องการฟังก์ชั่นนี้สำหรับการทำงาน:
//Finds y value of given object
function findPos(obj) {
var curtop = 0;
if (obj.offsetParent) {
do {
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return [curtop];
}
}
หน้าจอจะถูกเลื่อนไปยังองค์ประกอบเฉพาะของคุณ
window
คุณต้องการเลื่อนไม่ใช่พื้นที่รับชมมาก
[curtop]
ไปcurtop
ในตอนท้าย
(window.screen.height/2)
จาก findPos
ฉันได้รับการดูเล็กน้อยในนี้และฉันคิดนี้ออกซึ่งอย่างใดรู้สึกเหมือนวิธีธรรมชาติที่สุดในการทำ แน่นอนว่านี่คือการเลื่อนที่ชื่นชอบส่วนตัวของฉันตอนนี้ :)
const y = element.getBoundingClientRect().top + window.scrollY;
window.scroll({
top: y,
behavior: 'smooth'
});
โปรดทราบว่าwindow.scroll({ ...options })
ไม่รองรับ IE, Edge และ Safari ในกรณีดังกล่าวน่าจะใช้งานelement.scrollIntoView()
ได้ดีที่สุด
(รองรับบน IE 6) คุณสามารถมีโอกาสมากที่สุด (อ่าน: ยังไม่ทดลอง) ผ่านในตัวเลือกโดยไม่มีผลข้างเคียงใด ๆ
แน่นอนว่าสิ่งเหล่านี้สามารถห่อหุ้มในฟังก์ชั่นที่ทำงานตามเบราว์เซอร์ที่ใช้งานอยู่
window.scroll
ลองสิ่งนี้:
var divFirst = document.getElementById("divFirst");
divFirst.style.visibility = 'visible';
divFirst.style.display = 'block';
divFirst.tabIndex = "-1";
divFirst.focus();
เช่น @:
element.tabIndex
ไม่ใช่ แต่element.tabindex
; อันที่สองใช้ได้กับ Firefox แต่ไม่ใช่ใน Chrome (อย่างน้อยตอนที่ฉันลองมาแล้ว) แน่นอนใช้เป็นแอตทริบิวต์ HTML ทั้งtabIndex
และtabindex
ทำงาน (และใน XHTML tabindex
ต้องใช้)
หากต้องการเลื่อนไปที่องค์ประกอบที่กำหนดให้สร้างจาวาสคริปต์นี้เฉพาะโซลูชันด้านล่าง
การใช้งานง่าย:
EPPZScrollTo.scrollVerticalToElementById('signup_form', 20);
วัตถุเครื่องยนต์ (คุณสามารถเล่นซอกับฟิลเตอร์, ค่า fps):
/**
*
* Created by Borbás Geri on 12/17/13
* Copyright (c) 2013 eppz! development, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
var EPPZScrollTo =
{
/**
* Helpers.
*/
documentVerticalScrollPosition: function()
{
if (self.pageYOffset) return self.pageYOffset; // Firefox, Chrome, Opera, Safari.
if (document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop; // Internet Explorer 6 (standards mode).
if (document.body.scrollTop) return document.body.scrollTop; // Internet Explorer 6, 7 and 8.
return 0; // None of the above.
},
viewportHeight: function()
{ return (document.compatMode === "CSS1Compat") ? document.documentElement.clientHeight : document.body.clientHeight; },
documentHeight: function()
{ return (document.height !== undefined) ? document.height : document.body.offsetHeight; },
documentMaximumScrollPosition: function()
{ return this.documentHeight() - this.viewportHeight(); },
elementVerticalClientPositionById: function(id)
{
var element = document.getElementById(id);
var rectangle = element.getBoundingClientRect();
return rectangle.top;
},
/**
* Animation tick.
*/
scrollVerticalTickToPosition: function(currentPosition, targetPosition)
{
var filter = 0.2;
var fps = 60;
var difference = parseFloat(targetPosition) - parseFloat(currentPosition);
// Snap, then stop if arrived.
var arrived = (Math.abs(difference) <= 0.5);
if (arrived)
{
// Apply target.
scrollTo(0.0, targetPosition);
return;
}
// Filtered position.
currentPosition = (parseFloat(currentPosition) * (1.0 - filter)) + (parseFloat(targetPosition) * filter);
// Apply target.
scrollTo(0.0, Math.round(currentPosition));
// Schedule next tick.
setTimeout("EPPZScrollTo.scrollVerticalTickToPosition("+currentPosition+", "+targetPosition+")", (1000 / fps));
},
/**
* For public use.
*
* @param id The id of the element to scroll to.
* @param padding Top padding to apply above element.
*/
scrollVerticalToElementById: function(id, padding)
{
var element = document.getElementById(id);
if (element == null)
{
console.warn('Cannot find element with id \''+id+'\'.');
return;
}
var targetPosition = this.documentVerticalScrollPosition() + this.elementVerticalClientPositionById(id) - padding;
var currentPosition = this.documentVerticalScrollPosition();
// Clamp.
var maximumScrollPosition = this.documentMaximumScrollPosition();
if (targetPosition > maximumScrollPosition) targetPosition = maximumScrollPosition;
// Start animation.
this.scrollVerticalTickToPosition(currentPosition, targetPosition);
}
};
นี่คือฟังก์ชั่นที่สามารถรวมออฟเซ็ตเสริมสำหรับส่วนหัวคงที่เหล่านั้น ไม่จำเป็นต้องใช้ไลบรารีภายนอก
function scrollIntoView(selector, offset = 0) {
window.scroll(0, document.querySelector(selector).offsetTop - offset);
}
คุณสามารถคว้าความสูงขององค์ประกอบโดยใช้ JQuery และเลื่อนไปที่มัน
var headerHeight = $('.navbar-fixed-top').height();
scrollIntoView('#some-element', headerHeight)
อัปเดตมีนาคม 2561
เลื่อนไปที่คำตอบนี้โดยไม่ใช้ JQuery
scrollIntoView('#answer-44786637', document.querySelector('.top-bar').offsetHeight)
คุณสามารถกำหนดโฟกัสไปที่องค์ประกอบ มันทำงานได้ดีกว่าscrollIntoView
node.setAttribute('tabindex', '-1')
node.focus()
node.removeAttribute('tabindex')
คำตอบสั้น ๆ ที่ดีที่สุดว่าอะไรที่ใช้งานได้กับเอฟเฟกต์ภาพเคลื่อนไหว:
var scrollDiv = document.getElementById("myDiv").offsetTop;
window.scrollTo({ top: scrollDiv, behavior: 'smooth'});
หากคุณมีแถบนำทางคงที่ให้ลบความสูงออกจากค่าสูงสุดดังนั้นหากความสูงของแถบคงที่คือ 70px บรรทัดที่ 2 จะมีลักษณะดังนี้:
window.scrollTo({ top: scrollDiv-70, behavior: 'smooth'});
คำอธิบาย:
บรรทัดที่ 1 รับตำแหน่งองค์ประกอบบรรทัดที่ 2 เลื่อนไปที่ตำแหน่งองค์ประกอบ behavior
คุณสมบัติเพิ่มเอฟเฟกต์ภาพเคลื่อนไหวที่ราบรื่น
สามารถกำหนดโฟกัสได้ที่องค์ประกอบแบบอินเทอร์แอคทีฟเท่านั้น ... Div เท่านั้นที่แสดงเฉพาะส่วนตรรกะของหน้า
บางทีคุณสามารถกำหนดเส้นขอบรอบ ๆ div หรือเปลี่ยนสีเพื่อจำลองโฟกัส และใช่การมองเห็นไม่ได้มุ่งเน้น
ฉันคิดว่าถ้าคุณเพิ่ม tabindex ให้ div ของคุณมันจะสามารถได้รับการโฟกัส:
<div class="divFirst" tabindex="-1">
</div>
ฉันคิดว่ามันไม่ถูกต้อง แต่ tabindex สามารถใช้ได้เฉพาะกับพื้นที่ปุ่มอินพุตวัตถุเลือกและ textarea เท่านั้น แต่ลองดูสิ
tabindex
คือ "แอตทริบิวต์หลัก" ซึ่งเป็น "คุณสมบัติทั่วโลก" (คุณลักษณะที่ใช้ร่วมกับองค์ประกอบทั้งหมดในภาษา HTML) ดูw3.org/TR/2011/WD-html-markup-20110113/global-attributes.html
คล้ายกับโซลูชันของ @ caveman
const element = document.getElementById('theelementsid');
if (element) {
window.scroll({
top: element.scrollTop,
behavior: 'smooth',
})
}
คุณไม่สามารถมุ่งเน้นไปที่กอง คุณสามารถมุ่งเน้นไปที่องค์ประกอบอินพุตใน div นั้นเท่านั้น นอกจากนี้คุณต้องใช้ element.focus () แทน display ()
<div>
โฟกัสได้หากใช้แอtabindex
ททริบิวต์ ดูdev.w3.org/html5/spec-author-view/editing.html#attr-tabindex
หลังจากมองไปรอบ ๆ นี่เป็นสิ่งที่ฉันได้ทำงานในที่สุด:
ค้นหา / ระบุ div ในโดมที่มีแถบเลื่อน สำหรับฉันมันดูเหมือนว่า: "div class =" table_body table_body_div "scroll_top =" 0 "scroll_left =" 0 "style =" width: 1263px; ความสูง: 499px; "
ฉันพบกับ xpath นี้: // div [@ class = 'table_body table_body_div']
ใช้ JavaScript เพื่อดำเนินการเลื่อนดังนี้: ไดรเวอร์ (JavascriptExecutor)) .executeScript ("อาร์กิวเมนต์ [0] .scrollLeft = อาร์กิวเมนต์ [1];", องค์ประกอบ, 2000);
2000 คือจำนวนพิกเซลที่ฉันต้องการเลื่อนไปทางขวา ใช้ scrollTop แทน scrollLeft หากคุณต้องการเลื่อน div ลง
หมายเหตุ: ฉันพยายามใช้ scrollIntoView แต่ทำงานไม่ถูกต้องเนื่องจากหน้าเว็บของฉันมีหลาย divs มันจะทำงานถ้าคุณมีหน้าต่างหลักเพียงหน้าต่างเดียวที่โฟกัสอยู่ นี่เป็นทางออกที่ดีที่สุดที่ฉันเจอถ้าคุณไม่ต้องการใช้ jQuery ที่ฉันไม่ต้องการ
วิธีที่ฉันมักจะใช้เพื่อเลื่อนภาชนะไปยังเนื้อหาของมัน
/**
@param {HTMLElement} container : element scrolled.
@param {HTMLElement} target : element where to scroll.
@param {number} [offset] : scroll back by offset
*/
var scrollAt=function(container,target,offset){
if(container.contains(target)){
var ofs=[0,0];
var tmp=target;
while (tmp!==container) {
ofs[0]+=tmp.offsetWidth;
ofs[1]+=tmp.offsetHeight;
tmp=tmp.parentNode;
}
container.scrollTop = Math.max(0,ofs[1]-(typeof(offset)==='number'?offset:0));
}else{
throw('scrollAt Error: target not found in container');
}
};
หากคุณต้องการแทนที่ทั่วโลกคุณสามารถทำได้:
HTMLElement.prototype.scrollAt=function(target,offset){
if(this.contains(target)){
var ofs=[0,0];
var tmp=target;
while (tmp!==this) {
ofs[0]+=tmp.offsetWidth;
ofs[1]+=tmp.offsetHeight;
tmp=tmp.parentNode;
}
container.scrollTop = Math.max(0,ofs[1]-(typeof(offset)==='number'?offset:0));
}else{
throw('scrollAt Error: target not found in container');
}
};
เนื่องจากพฤติกรรม "ราบรื่น" ไม่ทำงานใน Safari, Safari ios, Explorer ฉันมักจะเขียนฟังก์ชั่นง่าย ๆ โดยใช้ requestAnimationFrame
(function(){
var start;
var startPos = 0;
//Navigation scroll page to element
function scrollTo(timestamp, targetTop){
if(!start) start = timestamp
var runtime = timestamp - start
var progress = Math.min(runtime / 700, 1)
window.scroll(0, startPos + (targetTop * progress) )
if(progress >= 1){
return;
}else {
requestAnimationFrame(function(timestamp){
scrollTo(timestamp, targetTop)
})
}
};
navElement.addEventListener('click', function(e){
var target = e.target //or this
var targetTop = _(target).getBoundingClientRect().top
startPos = window.scrollY
requestAnimationFrame(function(timestamp){
scrollTo(timestamp, targetTop)
})
}
})();
ในกรณีที่คุณต้องการใช้ html คุณสามารถใช้สิ่งนี้:
a href="samplewebsite.com/subdivision.html#id
และทำให้เป็นลิงก์ html ไปยัง id องค์ประกอบเฉพาะ มันเป็นgetElementById
เวอร์ชั่น HTML โดยทั่วไป
ลองใช้ฟังก์ชั่นนี้
function navigate(divId) {
$j('html, body').animate({ scrollTop: $j("#"+divId).offset().top }, 1500);
}
ผ่าน id id เป็นพารามิเตอร์มันจะใช้งานได้แล้ว
$j
มาจากไหน
visibility
และdisplay
ใช้สำหรับการสร้างองค์ประกอบ (ใน) ที่มองเห็นได้ คุณต้องการเลื่อน div ในหน้าจอหรือไม่?