I have the following code in js and htm.
<div id="div1_myDiv">This is my div1</div>
<div id="div2_myDiv">This is my div2</div>
<div id="div3_myDiv">This is my div3</div>
<div id="button1" onclick="openDiv('div1')">Show div1</div>
<div id="button2" onclick="openDiv('div2')">Show div2</div>
<div id="button3" onclick="openDiv('div3')">Show div3</div>
Whenever a button(div) is clicked, the page should scroll to the top of the div.
function openDiv(divID) {
var myDivID = "#" + divID + "_myDiv";
window.location.href = myDivID;
}
This doesn't seem to work. Please advise a solution.
This question was marked as a duplicate of div scroll to in javascript but that didn't help.
I have the following code in js and htm.
<div id="div1_myDiv">This is my div1</div>
<div id="div2_myDiv">This is my div2</div>
<div id="div3_myDiv">This is my div3</div>
<div id="button1" onclick="openDiv('div1')">Show div1</div>
<div id="button2" onclick="openDiv('div2')">Show div2</div>
<div id="button3" onclick="openDiv('div3')">Show div3</div>
Whenever a button(div) is clicked, the page should scroll to the top of the div.
function openDiv(divID) {
var myDivID = "#" + divID + "_myDiv";
window.location.href = myDivID;
}
This doesn't seem to work. Please advise a solution.
This question was marked as a duplicate of div scroll to in javascript but that didn't help.
Share Improve this question edited May 23, 2017 at 11:58 CommunityBot 11 silver badge asked May 24, 2016 at 7:37 AfzAfz 772 silver badges16 bronze badges 6- Do you want to scroll the window (you use an anchor)? To open a popup (that's the function's title)? Please describe your goal more precisely. – A.L Commented Apr 29, 2016 at 11:49
- Yes. Need to scroll the window to the top of the popup. – Afz Commented Apr 29, 2016 at 11:51
- Is the code executed from the popup or from the parent window? – A.L Commented Apr 29, 2016 at 11:53
-
2
have you tried
window.location.hash = myDivID
– vijayP Commented May 24, 2016 at 7:41 - Works good here – Guruprasad J Rao Commented May 24, 2016 at 7:43
6 Answers
Reset to default 3Could you please modify your function openDiv()
in following way:
function openDiv(divID) {
var myDivID = "#" + divID + "_myDiv";
window.location.hash = myDivID; //setting the location.hash
}
Try this With the animation
function openDiv() {
var myDivID = "#" + divID + "_myDiv";
$('html, body').animate({ scrollTop: $(myDivID).position().top}, 1000);
});
If you just need to scroll to the top of your window window.location.href = "#"
will do just fine.
function popitup(url,windowName) {
newwindow=window.open(url,windowName,'height=200,width=150');
if (window.focus) {newwindow.focus()}
return false;
}
"window.location.href" is use to change the current web page of the browser. for your task, you can use the anchor tag element to show the div section. just like below,
<div id="div1_myDiv">This is my div1</div>
<div id="div2_myDiv">This is my div2</div>
<div id="div3_myDiv">This is my div3</div>
...
<a id="button1" href="#div1_myDiv">Show div1</a>
<a id="button2" href="#div2_myDiv">Show div1</a>
<a id="button3" href="#div3_myDiv">Show div1</a>
You need to concate
myDivID in existing URL
.
Try this solution
function openDiv(divID) {
//alert(divID);
var myDivID = "#" + divID + "_myDiv";
//window.location.hash = myDivID;
window.location.href=window.location.href.concat('#Id');
console.log(window.location.href);
}
<div id="div1_myDiv">This is my div1</div>
<div id="div2_myDiv">This is my div2</div>
<div id="div3_myDiv">This is my div3</div>
...
<div id="button1" onclick="openDiv('div1')">Show div1</div>
<div id="button2" onclick="openDiv('div2')">Show div2</div>
<div id="button3" onclick="openDiv('div3')">Show div3</div>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744280242a4566532.html
评论列表(0条)