网页学习体会

  • 首页
  • 个人博客
您的位置: 首页  >  IT文章  >  哪位高手能帮小弟我写一个
跟着浏览器范围而移动效果【100分求js代码】

哪位高手能帮小弟我写一个
跟着浏览器范围而移动效果【100分求js代码】

分类: IT文章 • 2022-07-26 00:24:32
谁能帮我写一个<div>跟着浏览器范围而移动效果【100分求js代码】
最好用jQuery写

效果如图所示:
1、原始状态
哪位高手能帮小弟我写一个<div>跟着浏览器范围而移动效果【100分求js代码】
2、浏览器向下滚动
哪位高手能帮小弟我写一个<div>跟着浏览器范围而移动效果【100分求js代码】
3、继续向下
哪位高手能帮小弟我写一个<div>跟着浏览器范围而移动效果【100分求js代码】
4、浏览器向上滚动
哪位高手能帮小弟我写一个<div>跟着浏览器范围而移动效果【100分求js代码】
------解决方案--------------------
调用 document.documentElement.offsetTop 就可以,这个是你浏览器当前内容距离顶部的坐标

onscroll 和 onresize 你自己定义一下就可以
------解决方案--------------------
对了,div 记得 position:absolute
------解决方案--------------------
用position:fixed  试试呢
------解决方案--------------------
抄来的代码,参考下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>固定浮动侧边栏</title>
<style type="text/css">
body {
margin: 10px auto;
font-family: sans-serif;
width: 500px;
}

div {
border-radius: 5px;
box-shadow: 1px 2px 5px rgba(0,0,0,0.3);
border: 1px solid #ccc;
padding: 5px;
}

#sidebarWrap {
height: 400px;
width: 210px;
float: right;
position: relative;
box-shadow: none;
border: none;
margin: 0;
padding: 0;
}

#main {
width: 270px;
height: 4000px;
}

#footer {
clear: both;
margin: 10px 0;
}

#sidebar {
width: 200px;
height: 300px;
position: absolute;
}

#header {
height: 200px;
margin-bottom: 10px;
}

#sidebar.fixed {
position: fixed;
top: 0;
}

#footer { height: 600px; }
</style>
</head>

<body>
    <div id="header">header</div>
    
    <div id="sidebarWrap">
        <div id="sidebar">Sidebar</div>
    </div>
    
    <div id="main">
        Main
    </div>
    
    <div id="footer">
        footer
    </div>
</body>
</html>
<script src="../Js/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
    var top = $('#sidebar').offset().top - parseFloat($('#sidebar').css('marginTop').replace(/auto/, 0));
    var footTop = $('#footer').offset().top - parseFloat($('#footer').css('marginTop').replace(/auto/, 0));

    var maxY = footTop - $('#sidebar').outerHeight();

    $(window).scroll(function(evt) {
        var y = $(this).scrollTop();
        if (y > top) {
            if (y < maxY) {
                $('#sidebar').addClass('fixed').removeAttr('style');
            } else {
                $('#sidebar').removeClass('fixed').css({
                    position: 'absolute',
                    top: (maxY - top) + 'px'
                });
            }
        } else {
            $('#sidebar').removeClass('fixed');
        }
    });
});
</script>

------解决方案--------------------
完成

剛幫你客製化寫了個: http://jsfiddle.net/w7txx/6/

原碼請照實際情況去修改

相关推荐

  • 哪位高手能帮小弟我写一个
    跟着浏览器范围而移动效果【100分求js代码】
  • 高手帮忙看看js,马上结帐解决思路
  • URL调用js的具体方法,该如何处理
    网站免责声明 网站地图 最新文章 用户隐私 版权申明
本站所有数据收集于网络,如果侵犯到您的权益,请联系网站进行下架处理。   

Copyright © 2018-2021   Powered By 网页学习体会    备案号:   粤ICP备20002247号