मैं पृष्ठ को एक <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"और काम भी करता है।
स्क्रॉल इन व्यू अच्छा काम करता है:
document.getElementById("divFirst").scrollIntoView();
एमडीएन डॉक्स में पूरा संदर्भ:
https://developer.mozilla.org/en-US/docs/Web/API/Element.scrollIntoView
आपका प्रश्न और उत्तर अलग दिखता है। मुझे नहीं पता कि मैं गलत हूं, लेकिन उन लोगों के लिए जो गुगली करते हैं और यहां तक पहुंचते हैं, मेरा जवाब निम्नलिखित होगा:
मेरा उत्तर समझाया गया:
यहाँ उस के लिए एक सरल जावास्क्रिप्ट है
जब आप आईडी = "yourSpecificElementId" वाले तत्व को स्क्रीन स्क्रॉल करने की आवश्यकता हो तो इसे कॉल करें
window.scroll(0,findPos(document.getElementById("yourSpecificElementId")));
अर्थात। उपरोक्त प्रश्न के लिए, यदि आईडी के लिए स्क्रीन को div '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, एज और सफारी पर समर्थित नहीं है। उस स्थिति में इसका उपयोग करना सबसे अच्छा है
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; दूसरा फ़ायरफ़ॉक्स पर काम करता है लेकिन क्रोम पर नहीं (कम से कम जब मैंने कुछ समय पहले इसे आज़माया था)। बेशक, HTML विशेषता के रूप में उपयोग किया जाता है tabIndexऔर tabindexकाम (और XHTML पर, tabindexउपयोग किया जाना चाहिए)
किसी दिए गए तत्व को स्क्रॉल करने के लिए, बस इस जावास्क्रिप्ट को केवल नीचे दिए गए समाधान से बनाएं।
सरल उपयोग:
EPPZScrollTo.scrollVerticalToElementById('signup_form', 20);
इंजन ऑब्जेक्ट (आप फ़िल्टर, एफपीएस मानों के साथ फील कर सकते हैं):
/**
*
* 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)
अपडेट मार्च 2018
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 के चारों ओर की सीमाओं को सेट कर सकते हैं या फ़ोकस को अनुकरण करने के लिए इसे बदल सकते हैं। और हां विज़िबिलिटी फोकस नहीं है।
मुझे लगता है कि यदि आप अपने div में एक tabindex जोड़ते हैं, तो यह ध्यान केंद्रित करने में सक्षम होगा:
<div class="divFirst" tabindex="-1">
</div>
मुझे नहीं लगता कि यह मान्य है, लेकिन टैबइंडेक्स को केवल ए, क्षेत्र, बटन, इनपुट, ऑब्जेक्ट, सिलेक्ट और टेक्सारिया पर लागू किया जा सकता है। लेकिन यह एक कोशिश दे।
tabindexमें एक "मुख्य विशेषता" है, जो "वैश्विक विशेषताएँ" हैं (विशेषताएँ जो HTML भाषा के सभी तत्वों के लिए सामान्य हैं)। देखें w3.org/TR/2011/WD-html-markup-20110113/global-attributes.html
@ गुफाओं के समाधान के समान
const element = document.getElementById('theelementsid');
if (element) {
window.scroll({
top: element.scrollTop,
behavior: 'smooth',
})
}
आप एक div पर ध्यान केंद्रित नहीं कर सकते। आप केवल उस div में एक इनपुट तत्व पर ध्यान केंद्रित कर सकते हैं। इसके अलावा, आपको प्रदर्शन के बजाय element.focus () का उपयोग करने की आवश्यकता है ()
<div>का उपयोग कर ध्यान केंद्रित कर सकते हैं tabindex। देखें dev.w3.org/html5/spec-author-view/editing.html#attr-tabindex
बहुत कुछ देखने के बाद, आखिरकार यह मेरे लिए काम कर रहा है:
अपने डोम में div / ढूँढें जो स्क्रॉल बार है। मेरे लिए, यह इस तरह दिखता था: "div class =" table_body table_body_div "स्क्रॉल_टॉप =" 0 "स्क्रॉल_लेफ्ट =" 0 "शैली =" चौड़ाई: 1263px; ऊंचाई: 499px; "
मैंने इसे इस xpath के साथ स्थित किया है: // div [@ class = 'table_body table_body_div']
इस तरह स्क्रॉलिंग को निष्पादित करने के लिए जावास्क्रिप्ट का उपयोग किया: (JavascriptExecutor) ड्राइवर) .executeScript ("तर्क [0] [.scrollLeft = तर्क [1];", तत्व, 2000)।
2000 में पिक्सेल की कोई संख्या नहीं है जिसे मैं दाईं ओर स्क्रॉल करना चाहता था। यदि आप अपने डिव को नीचे स्क्रॉल करना चाहते हैं, तो स्क्रॉललेट के बजाय स्क्रॉलटॉप का उपयोग करें।
नोट: मैंने स्क्रॉल इन्टाउट का उपयोग करने की कोशिश की, लेकिन यह ठीक से काम नहीं कर पाया क्योंकि मेरे वेबपेज में कई विभाग थे। यह काम करेगा यदि आपके पास केवल एक मुख्य विंडो है, जहां फोकस झूठ है। यदि आप 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');
}
};
व्यवहार के कारण "सुचारू" सफारी, सफारी आईओएस, एक्सप्लोरर में काम नहीं करता है। मैं आमतौर पर अनुरोध का उपयोग करते हुए एक साधारण फ़ंक्शन लिखता हूं
(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)
})
}
})();
इस फ़ंक्शन का प्रयास करें
function navigate(divId) {
$j('html, body').animate({ scrollTop: $j("#"+divId).offset().top }, 1500);
}
डिव आईडी को पैरामीटर के रूप में पास करें यह काम करेगा मैं पहले से ही इसका उपयोग कर रहा हूं
$j?
visibilityऔरdisplayतत्व (इन) दृश्यमान बनाने के लिए उपयोग किए जाते हैं। क्या आप स्क्रीन में div स्क्रॉल करना चाहते हैं?