面向对象吸顶条

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{margin:0;padding:0;}
        .Suction_cap{width:100%;height:100px;overflow:hidden;background:skyblue;}
        .main{width:1200px;height:1500px;}
    </style>
</head>
<body>
<div class="box">
    <div id="Suction_cap" class="Suction_cap">
        头部
    </div>
    <div class="main">
           测试<br/>
           测试<br/>
           测试<br/>
           测试<br/>
           测试<br/>
    </div>
</div>
<script>
    function Suction_cap(obj){
        this.ele=document.getElementById(obj)||document.getElementsByClassName(obj)[0];
        this.height=this.ele.offsetHeight;
        this.copy=document.createElement(‘div‘);
        this.parent_ele=this.ele.parentNode;
        var _this=this;
        window.onscroll=function(){
            var scrollT = document.documentElement.scrollTop || document.body.scrollTop;
            if(scrollT>0){
                _this.insertAfter( _this.copy, _this.ele);
                _this.copy.style.height=_this.getStyle(_this.ele,‘height‘);
                _this.copy.style.display=‘block‘;
                _this.ele.style.position=‘fixed‘;
                _this.ele.style.boxShadow="0 0 5px #888";
            }else{
                _this.parent_ele.removeChild(_this.copy);
                _this.ele.style.display=‘block‘;
                _this.ele.style.position=‘relative‘;
                _this.ele.style.boxShadow="0 0 0 #fff";
            }
        }
    }
    Suction_cap.prototype.getStyle=function(obj,attr){
        if(obj.currentStyle)
        {
            return obj.currentStyle[attr];
        }
        else
        {
            return getComputedStyle(obj, false)[attr];
        }
    }
    Suction_cap.prototype.insertAfter=function( newElement, targetElement){
        var parent = targetElement.parentNode;
        if ( parent.lastChild == targetElement )
        {
            parent.a( newElement );
        }
        else
        {
            parent.insertBefore( newElement, targetElement.nextSibling );
        }
    }
    var obj=new Suction_cap(‘Suction_cap‘);
</script>
</body>
</html>

  

时间: 2024-10-24 05:31:32

面向对象吸顶条的相关文章

进度条、输入框文字提示、图片延迟加载、吸顶条

进度条:                            window.onload=function(){                                            var oBox1=document.getElementById("box1");                                            var oBox2=document.getElementById("box2");      

吸顶条 ---- jQ写法

<script> $(function () { var barTop = $('#bar').offset().top; //on方法相当于原生的绑定 $(window).on('scroll',function () { var scrollTop = $(document).scrollTop(); if (scrollTop >= barTop) { //固定在上方 $('#bar').css({position: 'fixed',top: 0}); // 显示替代的条 $('#

吸顶条+大图加载滚动

<!doctype html><html><head><meta charset="utf-8"><title>无标题文档</title><style>*{margin:0;padding:0;}.box{ position:relative;padding:50px; border-bottom:1px solid red;}.box img{ display:block;width:1200px;h

吸顶条或者其他的吧

<!DOCTYPE html><html><head><style>*{margin: 0;padding: 0;list-style: none;}body{ width: 1180px; margin: 0 auto;}.head{ width: 100%; height: 60px; background: red;}.main{ margin: 30px 0 ; height: 100%; overflow: hidden; }.foot{ widt

原生js吸顶式导航条

<!--吸顶式导航条--><nav class="nav"> <div> <a href="javascript: return false"><img src="n.jpg" ></a> <div class="topNav"> <div> <input type="text" placeholder=&

原生js实现吸顶导航和回到顶部特效

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>吸顶导航特效</title> 6 <style type="text/css"> 7 body{ 8 margin: 0; 9 padding: 0; 10 } 11 .main{ 12 width: 10

js实现导航栏的吸顶操作

1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 <style type="text/css&

5行js代码搞定导航吸顶效果

一.HTML布局 首先写HTML布局 <body> <div id="wrap"></div> </body> 二.CSS样式 给点简单的样式 <style> *{ margin: 0; padding: 0; } body{ height: 2000px; background-image: linear-gradient(-180deg, #15f09d 0%, #25A0FF 50%, #fca72b 100%); }

代码:jquery 吸顶 效果

(只是个简单吸顶效果,还需完善) 吸顶: <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $(window).scroll( function() { var m =$(document).scrollTop(); console.log(