滚动/跳转到没有jQuery的id [英] Scroll / Jump to id without jQuery
问题描述
我已经搜索了很多可用解决方案的主题。但是没有找到任何东西。大多数脚本对我来说太混乱了。寻求一种仅基于Javascript的解决方案。 在我的项目中,不能使用jQuery。
我需要跳转或滚动到id。
< head>
< script type =text / javascript>
//此处必须运行
< / script>
< / head>
我的电话是:
< a onclick =function_call + targetname>< img src =image1alt =name1>< / a>
< a onclick =function_call + targetname>< img src =image2alt =name2>< / a>
< a onclick =function_call + targetname>< img src =image3alt =name3>< / a>
所以我必须在我的导航中使用onclick事件。
现在onclick我想跳转或滚动到我的页面中的div id:
< div id = target1>某些内容< / div>
< div id =target2>某些内容< / div>
< div id =target3>某些内容< / div>
这非常重要:html锚不工作不幸。否则一切都会很容易。
我简单地使用了bevore:
onclick =window.location.hash ='target';
但是我在eBay有限制。他们不允许这个简单的代码。
另外我不能调用一个外部JavaScript(只在头部区域使用JS)。此外,无法使用:cookie,cookie,replace(,IFRAME,META或includes)和base href。
它可以不要用一点JS跳到特定的点。
我不需要特殊效果。
有没有人有苗条而有用的解决方案?
我自己找到了解决方案,并感谢Oxi。我遵循您的
方式。
对于所有对我的解决方案感兴趣的人:
< head> < script type =text / javascript> function scroll(element){
var ele = document.getElementById(element);
window.scrollTo(ele.offsetLeft,ele.offsetTop); }< / script> < /头>
导航电话:
< a onclick ='scroll(target1);'>< img src =image1alt =name1 >< / A>
现在您可以使用被叫号码跳转到一个div
< div id =target1> CONTENT< / div>
也许您应该尝试scrollIntoView。
document.getElementById('id')。scrollIntoView();
这将滚动到您的元素。
I have search a lot of topics for a usable solution.
But dont found something. Most scripts are just too cluttered for my purposes.
Seeking a solution based only on Javascript. In my project it is not possible to use jQuery.
I need a jump or scroll to id.
<head>
<script type="text/javascript">
//here has to go function
</script>
</head>
MY call is:
<a onclick="function_call+targetname"><img src="image1" alt="name1"></a>
<a onclick="function_call+targetname"><img src="image2" alt="name2"></a>
<a onclick="function_call+targetname"><img src="image3" alt="name3"></a>
So i have to use onclick event in my navigation.
Now onclick i want to jump or scroll to a div id in my page:
<div id="target1">some content</div>
<div id="target2">some content</div>
<div id="target3">some content</div>
It is very important: html anchor not work unfortunately. Otherwise everything would be pretty easy.
I have use bevore simply:
onclick="window.location.hash='target';"
But i have restrictions in eBay. They dont allow this simple code.
Also I cant call an external javascript (only use JS in head area). Furthermore, it is not possible to use : "cookie.", "cookie", "replace (", IFRAME, META or includes) and base href.
It can not be hard with a bit of JS jump to a specific point. I do not need special effects.
Does anyone have a slim and helpful solution?
I Have found a solution by my self and thanks to Oxi. I follow your way.
For all those interested in my solution:
<head> <script type="text/javascript"> function scroll(element){
var ele = document.getElementById(element);
window.scrollTo(ele.offsetLeft,ele.offsetTop); } </script> </head>
Navigation Call:
<a onclick='scroll("target1");'><img src="image1" alt="name1"></a>
Now you can jump to a div with called ID
<div id="target1">CONTENT</div>
Maybe You should try scrollIntoView.
document.getElementById('id').scrollIntoView();
This will scroll to your Element.
这篇关于滚动/跳转到没有jQuery的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!