Home > Article > Web Front-end > How to deal with page anchor failure in iframe
This time I will show you how to deal with page anchor failure in iframe. What are the precautions for handling page anchor failure in iframe? The following is a practical case, let's take a look.
The application scenario is: the iframe page does not have scroll bars. If a scroll bar appears in the parent form, the anchor mark will become invalid because the anchor scrolls the window based on the current window scroll bar and becomes a child form. If there is no scroll bar, it will naturally not scroll. The solution is: use js to determine whether the page is nested, use js to calculate the position of the iframe in the parent form, the position of the anchor point in the firame, and add the two to become the scrolling of the parent form. Encountered a problem: Get the parent form element (because of domain restrictions, all need to be in the network environment (ie http://domain.com)); the parent form nests multiple iframes, judge Whether it is the current iframe page. Code: Parent form page index.html<!doctype html> <html> <head> <title></title> <style type="text/css"> *{ margin: 0; padding: 0; border: 0; } html, body{ width: 100%; height: 100%; } </style> </head> <body> <p style="width:100%;background:#f00;height:500px;"></p> <a href="">dd</a> <a href="">ddd</a> <iframe name="iframe2" id="iframe2" src="iframe.html?a=b&c=d" style="width:100%;height:2060px;"></iframe> <iframe name="iframe2" id="iframe2" src="iframe.html?a=d&c=b" style="width:100%;height:2060px;"></iframe> </body> </html>Sub form page iframe.html
<!doctype html> <html> <head> <title></title> <style type="text/css"> a{ padding: 5px; border: 1px solid #f00; float: left; display: block; margin-right: 5px; } p{ width: 80%; margin: 10px auto; height: 500px; border: 1px solid #f00; font-size: 30px; } </style> <script type="text/javascript" src="jquery-1.8.2.min.js"></script> <script type="text/javascript"> $(function(){ //如果是iframe则需要在网络中访问,因为js里有域限制 //如果没有iframe则不进行处理, if(window!==window.top){ //获取top窗口中的iframe,如果有iframe嵌套过多,请自行修改 var iframeList=window.top.document.getElementsByTagName('iframe'); for(var i=0;i<iframeList.length;i++){ //判断当前窗口是否循环中的iframe if(window.location.toString().indexOf(iframeList[i].getAttribute('src').toString())!=-1){ //等自己的所在iframe加载完成给a锚点加事件 window.top.document.getElementsByTagName('iframe')[i].onload=function(){ //确定iframe在父窗体的距顶部距离 var top = window.top.document.getElementsByTagName('iframe')[i].offsetTop; $('a').each(function(){ var href = $(this).attr('href'); if(href.indexOf('#')!=-1){//判断是否是锚点而不是链接 var name = href.substring(href.indexOf('#')+1); $(this).bind('click',function(){ $('a').each(function(){ if($(this).attr('name')==name){ //父窗口滚动 $(window.parent).scrollTop($(this).offset().top+top); } }); }); } }); } } } } }); </script> </head> <body> <a href="#a" rel="external nofollow" >a</a> <a href="#b" rel="external nofollow" >b</a> <a href="#c" rel="external nofollow" >c</a> <a href="#d" rel="external nofollow" >d</a> <p><a href="" name=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" a">A</a></p> <p><a href="" name=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" b">B</a></p> <p><a href="" name=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" c">C</a></p> <p><a href="" name=" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" d">D</a></p> </body> </html>I believe you have mastered it after reading the case in this article For more exciting methods, please pay attention to other related articles on the php Chinese website! Recommended reading:
JS jquery library implements iframe height and width adaptation (with code)
How to trigger iframe Parent window element event
Tab scroll navigation switching implementation (with code)
The above is the detailed content of How to deal with page anchor failure in iframe. For more information, please follow other related articles on the PHP Chinese website!