├── LICENSE ├── README.md ├── bug_demo_5.html ├── fork.png ├── index.html ├── index1.html ├── index2.html ├── index3.html ├── index4.html ├── index5.html ├── index6.html ├── index7.html ├── index8.html ├── js ├── jquery.js ├── scrollfix-1.4.js ├── scrollfix.js └── scrollfix.min.js ├── reset.css └── test ├── bug_demo_5.html ├── followSidebar.js ├── hotfix ├── index.html ├── jquery-1.9.1.min.js └── scrollfix.js ├── index9.html ├── scrollfix (2).js ├── scrollfix.js ├── scrollfix2.js ├── test.html └── test2.html /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # scrollfix 2 | 3 | jquery插件:滑动到某个位置浮动起来 4 | 5 | [插件参数详解和在线演示](https://qdkfweb.cn/scrollfix) 6 | 7 | 8 | 插件参数有4个,下面我将说明一下如何使用这个浮动固定插件 9 | 10 | ### 第一个:startTop:null 11 | 12 | 这个是定义什么时候指定的元素开始浮动,如果没有设置,则默认滚动指定元素位置开始浮动。如有指定,可以为startTop:"#startTop",当元素到达这个标签的顶部时,则浮动出来。 13 | 14 | ### 第二个:startBottom:null 15 | 16 | 这个同样是定义元素什么时候开始浮动,跟上面不能冲突,只能指定一个。两个如果都指定,则默认后面这个参数为准。这个是当元素滚动到startBottom:"#startBottom"的末端时开始浮动。 17 | 18 | ### 第三个:distanceTop:0 19 | 20 | 距离顶部的高度,默认为0。 21 | 22 | ### 第四个:endPos:0 23 | 24 | 距离顶部的高度为多少时停止浮动,并固定在相应位置,可以指定jquery对象,如:endPos:"#footer",也可以是数字:endPos:300. 25 | 26 | ### 第五个:bottom:-1 27 | 28 | 默认不使用位置为bottom的值,当设置为0,将会停在窗口底端 29 | 30 | ### 第六个:zIndex:0 31 | 32 | 当没有设置这个值时,将默认使用元素的css z-index值 33 | 34 | ### 第七个:baseClassName:scrollfixed 35 | 36 | 当元素开始固定时给它添加一个class,你可以设置为你喜欢的类名。 37 | 38 | 39 | ### 使用方法 40 | 41 | $("#fix").scrollFix({startTop:"#startTop",distanceTop:20,endPos:"#footer"}); 42 | 43 | 具体你可以下载我里面提供的六个静态文件,给出了六种可能性。希望你会喜欢上这个插件。enjoy it! 44 | 45 | ### 贡献者 46 | 47 | 感谢“Plāybǒy”发现插件的bug,并把演示发给我。谢谢 48 | 49 | ### 版本 50 | 1.2 2013-10-29 51 | 52 | * 更新了startTop和startBottom的代码 53 | * 更新了在线演示代码 54 | 55 | 1.3 2014-6-17 56 | 57 | * 修复了当设置开始停止固定的高度大于元素距离底部高度时,不处理浮动固定 58 | * 修复了元素的高度加上margin-bottom的值 59 | * 修复了当元素没有触发position:fixed时,给元素增加一个填充元素的高度。 60 | 61 | 1.4 2014-9-4 62 | 63 | * 修复了上次开始停止固定的高度大于元素距离底部高度时,不处理浮动固定 64 | * 数值改为浮点数 65 | * 增加了对marginBottom为auto时的处理 66 | * 增加了两个混合的使用例子 67 | * 兼容IE6 68 | * 增加了对body有top值时修正固定的顶部高度 69 | 70 | 1.7 2014-9-29 71 | 72 | * 增加了三个参数,分别是bottom、zIndex和baseClassName. 73 | * 支持自适应布局,支持缩小窗口,即resize 74 | * 兼容IE6,支持上一级为position:relative或者position:absolute. 75 | * 如果没有什么重大的改动的话就是最后一个版本了。 76 | * 另外附上一个scrollfix-1.4.js的版本,这个是不支持自适应的最后一个版本,同时也没有以上加的三个参数。 77 | -------------------------------------------------------------------------------- /bug_demo_5.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 23 | 24 | 25 |
26 | 30 |
31 |
32 |

这只是内容,效果看侧栏

33 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
34 |
35 | 36 |
37 | 52 |
53 | 54 |
11111111
55 | 56 | 61 | 62 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /fork.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kujian/scrollfix/60c66e4e759a884d50952d8999ef84e11260d276/fork.png -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | scrollfix演示-前端博客 7 | 8 | 9 | 10 | 11 | 106 | 107 | 108 | 109 | 158 | 159 | 160 | 351 | Fork me on GitHub 352 | 353 | 354 | 355 | -------------------------------------------------------------------------------- /index1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 滚动到需要固定的地方开始固定-jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 23 | 24 | 25 |
26 | 30 |
31 |
32 |

开始浮动代码:这个是没有任何设置的,就是滚动到元素的位置开始浮动

33 |
<script type="text/javascript" src="js/scrollfix.min.js"></script>
 34 | <script type="text/javascript">
 35 | $(function(){
 36 | var fix = $(".fix");
 37 | fix.scrollFix();
 38 | })
 39 | </script>
40 |
41 |

这只是内容,效果看侧栏

42 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, culpa, officia, tempore, et vel earum delectus obcaecati ducimus at voluptates odio aliquid quis nulla illo veniam non dolorem molestias voluptas!
43 |
44 |
45 |

这只是内容,效果看侧栏

46 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
47 |
48 |
49 |

50 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
51 |
52 |
53 |

这只是内容,效果看侧栏

54 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
55 |
56 |
57 |

这只是内容,效果看侧栏

58 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
59 |
60 |
61 |

这只是内容,效果看侧栏

62 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
63 |
64 |
65 |

这是#fixFooter的位置

66 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
67 |
68 |
69 |

这是距离底部300像素位置

70 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
71 |
72 | 73 |
74 | 102 | 103 |
104 | 105 | 106 | 111 | 112 | 113 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /index2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 滚动到距离顶部20像素开始固定-jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 24 | 25 | 26 |
27 | 31 |
32 |
33 |

距离顶部20像素代码:滚动到距离浮动元素的顶部20像素位置开始固定

34 |
35 |
<script type="text/javascript" src="js/scrollfix.min.js"></script>
 36 | <script type="text/javascript">
 37 | $(function(){
 38 | var fixtop = $(".fix-top");
 39 | fixtop.scrollFix({distanceTop:20});
 40 | })
 41 | </script>
42 |
43 |
44 |
45 |

这只是内容,效果看侧栏

46 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, culpa, officia, tempore, et vel earum delectus obcaecati ducimus at voluptates odio aliquid quis nulla illo veniam non dolorem molestias voluptas!
47 |
48 |
49 |

这只是内容,效果看侧栏

50 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
51 |
52 |
53 |

这只是内容,效果看侧栏

54 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
55 |
56 |
57 |

这只是内容,效果看侧栏

58 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
59 |
60 |
61 |

这只是内容,效果看侧栏

62 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
63 |
64 |
65 |

这只是内容,效果看侧栏

66 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
67 |
68 |
69 |

这是#fixFooter的位置

70 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
71 |
72 |
73 |

这是距离底部300像素位置

74 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
75 |
76 | 77 |
78 | 106 | 107 |
108 | 109 | 110 | 115 | 116 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /index3.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 滚动到一个标签的头部开始固定-jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 24 | 25 | 26 |
27 | 31 |
32 |
33 |

滚动到某个标签的位置开始滚动

34 |
35 |

很多时候,你并不想滚动到该元素的时候开始滚动,而是在到达某个地方的时候才开始滚动,比如说评论时才出现等等,你可以随意定义这个开始固定的位置

36 |
<script type="text/javascript" src="js/scrollfix.min.js"></script>
 37 | <script type="text/javascript">
 38 | $(function(){
 39 | var fixStartTop = $(".fix-startTop");
 40 | fixStartTop.scrollFix({startTop:"#startTop"});
 41 | })
 42 | </script>
43 |
44 |
45 |
46 |

这只是内容,效果看侧栏

47 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, culpa, officia, tempore, et vel earum delectus obcaecati ducimus at voluptates odio aliquid quis nulla illo veniam non dolorem molestias voluptas!
48 |
49 |
50 |

这里是ID为startTop,也就是传说中右侧元素开始固定的位置啦

51 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
52 |
53 |
54 |

这只是内容,效果看侧栏

55 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
56 |
57 |
58 |

这只是内容,效果看侧栏

59 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
60 |
61 |
62 |

这只是内容,效果看侧栏

63 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
64 |
65 |
66 |

这只是内容,效果看侧栏

67 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
68 |
69 |
70 |

这是#fixFooter的位置

71 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
72 |
73 |
74 |

这是距离底部300像素位置

75 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
76 |
77 | 78 |
79 | 107 | 108 |
109 | 110 | 111 | 116 | 117 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /index4.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 滚动到一个标签的末端开始固定-jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 24 | 25 | 26 |
27 | 31 |
32 |
33 |

滚动到某个元素的末端才开始固定

34 |

很多时候,也许我们是需要在某个内容结束的位置接着出现固定,比如说侧栏固定内容,总得要侧栏的所有内容都看完了,才让他开始固定起来,否则就遮住了其他内容,效果就不太好了。代码如下:

35 |
<script type="text/javascript" src="js/scrollfix.min.js"></script>
 36 | <script type="text/javascript">
 37 | $(function(){
 38 | var fixStartBottom = $(".fix-startBottom");
 39 | fixStartBottom.scrollFix({startBottom:"#startBottom"});
 40 | })
 41 | </script>
42 |
43 |
44 |
45 |

这只是内容,效果看侧栏

46 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, culpa, officia, tempore, et vel earum delectus obcaecati ducimus at voluptates odio aliquid quis nulla illo veniam non dolorem molestias voluptas!
47 |
48 |
49 |

这只是内容,效果看侧栏

50 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
51 |
52 |
53 |

这只是内容,效果看侧栏

54 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
55 |
56 |
57 |

这只是内容,效果看侧栏

58 |
这是#startBottom的末端这是#startBottom的末端这是#startBottom的末端这是#startBottom的末端
59 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
60 |
61 |
62 |

这只是内容,效果看侧栏

63 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
64 |
65 |
66 |

这只是内容,效果看侧栏

67 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
68 |
69 |
70 |

这是#fixFooter的位置

71 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
72 |
73 |
74 |

这是距离底部300像素位置

75 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
76 |
77 | 78 |
79 | 107 | 108 |
109 | 110 | 111 | 116 | 117 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /index5.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 滚动停在底部300像素-jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 23 | 24 | 25 |
26 | 30 |
31 |
32 |

固定停留在底部的300像素位置

33 |

固定浮动的位置不能一直固定着,因为在它的底部经常有一些东西,到了某个位置之后我不需要它在固定了,我需要停在那里,否则的话可能会遮住了底部的东西

34 |
<script type="text/javascript" src="js/scrollfix.min.js"></script>
 35 | <script type="text/javascript">
 36 | $(function(){
 37 | var fixbottom = $(".fix-bottom");
 38 | fixbottom.scrollFix({endPos:300});
 39 | })
 40 | </script>
41 |
42 |
43 |
44 |

这只是内容,效果看侧栏

45 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, culpa, officia, tempore, et vel earum delectus obcaecati ducimus at voluptates odio aliquid quis nulla illo veniam non dolorem molestias voluptas!
46 |
47 |
48 |

这只是内容,效果看侧栏

49 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
50 |
51 |
52 |

这只是内容,效果看侧栏

53 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
54 |
55 |
56 |

这只是内容,效果看侧栏

57 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
58 |
59 |
60 |

这只是内容,效果看侧栏

61 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
62 |
63 |
64 |

这只是内容,效果看侧栏

65 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
66 |
67 |
68 |

这是#fixFooter的位置

69 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
70 |
71 |
72 |

这是距离底部300像素位置

73 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error, tempore, placeat, cupiditate soluta impedit inventore labore perferendis molestiae aut et blanditiis atque eaque
74 |
75 | 76 |
77 | 105 | 106 |
107 | 108 | 109 | 114 | 115 | 133 | 134 | 135 | -------------------------------------------------------------------------------- /index6.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 滚动停在底部某个标签的位置-jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 23 | 24 | 25 |
26 | 30 |
31 |
32 |

固定停留在某个标签位置

33 |
34 |

有时候你并不知道要停留在的位置距离底部有多少像素,你只知道它需要停在这个位置,那么你只需要知道这里的元素标签是什么就可以了

35 |
<script type="text/javascript" src="js/scrollfix.min.js"></script>
 36 | <script type="text/javascript">
 37 | $(function(){
 38 | var fixfooter = $(".fix-footer")
 39 | fixfooter.scrollFix({endPos:"#fixFooter"})
 40 | })
 41 | </script>
42 |
43 |
44 |
45 |

这只是内容,效果看侧栏

46 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, culpa, officia, tempore, et vel earum delectus obcaecati ducimus at voluptates odio aliquid quis nulla illo veniam non dolorem molestias voluptas!
47 |
48 |
49 |

这只是内容,效果看侧栏

50 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
51 |
52 |
53 |

这只是内容,效果看侧栏

54 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
55 |
56 |
57 |

这只是内容,效果看侧栏

58 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
59 |
60 |
61 |

这只是内容,效果看侧栏

62 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
63 |
64 |
65 |

这只是内容,效果看侧栏

66 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
67 |
68 |
69 |

这是#fixFooter底部开始停靠的位置

70 | 71 |
72 |
73 |

这只是内容,效果看侧栏

74 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
75 |
76 |
77 |

这只是内容,效果看侧栏

78 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
79 |
80 |
81 |

这只是内容,效果看侧栏

82 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
83 |
84 |
85 |

这只是内容,效果看侧栏

86 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
87 |
88 | 89 | 90 | 91 |
92 | 120 | 121 |
122 | 123 | 124 | 129 | 130 | 148 | 149 | 150 | -------------------------------------------------------------------------------- /index7.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 滚动停在底部300像素-jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 23 | 24 | 25 |
26 | 30 |
31 |
32 |

混合使用参数:1.元素距离顶部20像素,滚动到某个标签开始滚动,最后停靠在底部300像素位置

33 |
34 |

在日常中,我们常常需要的不是单独的某个,而是像上面的混合使用,没问题,同样支持

35 |
<script type="text/javascript" src="js/scrollfix.min.js"></script>
 36 | <script type="text/javascript">
 37 | $(function(){
 38 | var fixbottom = $(".fix-bottom");
 39 | fixbottom.scrollFix({distanceTop:20,startTop:"#startTop",endPos:300});
 40 | })
 41 | </script>
42 |
43 |
44 |
45 |

这只是内容,效果看侧栏

46 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, culpa, officia, tempore, et vel earum delectus obcaecati ducimus at voluptates odio aliquid quis nulla illo veniam non dolorem molestias voluptas!
47 |
48 |
49 |

这只是内容,效果看侧栏

50 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
51 |
52 |
53 |

这只是内容,效果看侧栏

54 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
55 |
56 |
57 |

这只是内容,效果看侧栏

58 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
59 |
60 |
61 |

这只是内容,效果看侧栏

62 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
63 |
64 |
65 |

这只是内容,效果看侧栏

66 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
67 |
68 |
69 |

这是#fixFooter的位置

70 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
71 |
72 |
73 |

这是距离底部300像素位置

74 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error, tempore, placeat, cupiditate soluta impedit inventore labore perferendis molestiae aut et blanditiis atque eaque
75 |
76 | 77 |
78 | 106 | 107 |
108 | 109 | 110 | 115 | 116 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /index8.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 滚动停在底部某个标签的位置-jquery滚动固定插件:scrollfix演示-前端博客 7 | 8 | 9 | 10 | 11 | 12 | 28 | 29 | 30 |
31 | 35 |
36 |
37 |

混合模式2:1.距离顶部10像素,2.到某个标签的底部开始浮动,停靠在某个标签的位置

38 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
39 |
40 |
41 |

这只是内容,效果看侧栏

42 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, culpa, officia, tempore, et vel earum delectus obcaecati ducimus at voluptates odio aliquid quis nulla illo veniam non dolorem molestias voluptas!
43 |
44 |
45 |

这是#startBottom的末端这是#startBottom的末端这是#startBottom的末端这是#startBottom的末端 46 | 这是#startBottom的末端这是#startBottom的末端这是#startBottom的末端这是#startBottom的末端 这是#startBottom的末端这是#startBottom的末端这是#startBottom的末端这是#startBottom的末端 这是#startBottom的末端这是#startBottom的末端这是#startBottom的末端这是#startBottom的末端

47 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
48 |
49 |
50 |

这只是内容,效果看侧栏

51 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
52 |
53 |
54 |

这只是内容,效果看侧栏

55 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
56 |
57 |
58 |

这只是内容,效果看侧栏

59 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
60 |
61 |
62 |

这只是内容,效果看侧栏

63 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
64 |
65 |
66 |

这是#fixFooter的位置

67 | 68 |
69 |
70 |

这只是内容,效果看侧栏

71 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
72 |
73 |
74 |

这只是内容,效果看侧栏

75 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
76 |
77 |
78 |

这只是内容,效果看侧栏

79 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
80 |
81 |
82 |

这只是内容,效果看侧栏

83 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
84 |
85 |
86 |

这只是内容,效果看侧栏

87 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
88 |
89 |
90 |

这只是内容,效果看侧栏

91 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
92 |
93 | 94 | 95 |
96 | 132 | 133 |
134 | 135 | 136 | 137 | 156 | 157 | 158 | -------------------------------------------------------------------------------- /js/scrollfix-1.4.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | //URI:https://qdkfweb.cn/scrollfix 3 | //author:caibaojian 4 | //website:https://qdkfweb.cn 5 | //descirption:scroll and fixed some div 6 | $.fn.scrollFix = function(options) { 7 | return this.each(function() { 8 | var defaults = { 9 | startTop: null, //滑到这个位置顶部时开始浮动,默认为空 10 | startBottom: null, //滑到这个位置末端开始浮动,默认为空 11 | distanceTop: 0, //固定在顶部的高度 12 | endPos: 0 //停靠在底部的位置,可以为jquery对象 13 | }; 14 | var opts = $.extend({}, 15 | defaults, options), 16 | obj = $(this), 17 | offset = obj.offset(), 18 | offsetTop = offset.top, //对象距离顶部高度 19 | offsetLeft = offset.left, //对象距离左边宽度 20 | placeholder = jQuery('
'), //创建一个jquery对象 21 | documentHeight = $(document).height(), //文档高度 22 | optsTop = opts.distanceTop, //定义到顶部的高度 23 | outerHeight = obj.outerHeight(), //对象高度 24 | outerHeight = parseFloat(outerHeight) + parseFloat(obj.css('marginBottom').replace(/auto/, 0)), 25 | outerWidth = obj.outerWidth(), //对象外宽度 26 | objWidth = obj.width(), 27 | startTop = $(opts.startTop), //开始浮动固定对象 28 | startBottom = $(opts.startBottom), 29 | toBottom, //停止滚动位置距离底部的高度 30 | ScrollHeight, //对象滚动的高度 31 | endfix; //开始停止固定的位置 32 | 33 | //如果没有找到节点,不进行处理 34 | if(obj.length<=0){ 35 | return; 36 | } 37 | var parents = obj.parent(); 38 | var Position = parents.css('position'); 39 | while(!/^relative|absolute$/i.test(Position)){ //检测浮动元素的父类元素定位为'relative'或者'absolute',是的话退出,否则的话,执行循环,继续寻找它的父类 40 | parents = parents.parent(); 41 | Position = parents.css('position'); 42 | if(/^body|html$/i.test(parents[0].tagName)) break; //假如父类元素的标签为body或者HTML,说明没有找到父类为以上的定位,退出循环 43 | } 44 | if(/^body|html$/i.test(parents[0].tagName)){ //当父类元素非body或者HTML时,说明找到了一个父类为'relative'或者'absolute'的元素,得出它的偏移高度 45 | var parentsOffset = {top:0,left:0}; 46 | }else{ 47 | var parentsOffset = parents.offset(); 48 | 49 | } 50 | 51 | // 计算父节点的上边到顶部距离 52 | // 如果 body 有 top 属性, 消除这些位移 53 | var bodyToTop = parseInt(jQuery('body').css('top'), 10); 54 | if(!isNaN(bodyToTop)) { 55 | optsTop += bodyToTop; 56 | } 57 | //计算停在底部的距离 58 | if (!isNaN(opts.endPos)) { 59 | toBottom = opts.endPos; 60 | } else { 61 | toBottom = parseFloat(documentHeight - $(opts.endPos).offset().top); 62 | } 63 | //计算需要滚动的高度以及停止滚动的高度 64 | ScrollHeight = parseFloat(documentHeight - toBottom), endfix = parseFloat(ScrollHeight - outerHeight); 65 | //计算顶部的距离值 66 | if (startTop[0]) { 67 | var startTopOffset = startTop.offset(), 68 | startTopPos = startTopOffset.top; 69 | offsetTop = startTopPos; 70 | } 71 | if (startBottom[0]) { 72 | var startBottomOffset = startBottom.offset(), 73 | startBottomPos = startBottomOffset.top, 74 | startBottomHeight = startBottom.outerHeight(); 75 | offsetTop = parseFloat(startBottomPos + startBottomHeight); 76 | } 77 | 78 | var toTop = parseFloat(offsetTop - optsTop); 79 | toTop = (toTop > 0) ? toTop : 0; 80 | var selfBottom = documentHeight - offsetTop - outerHeight; 81 | //如果滚动停在底部的值不为0,并且自身到底部的高度小于上面这个值,不执行浮动固定 82 | if((toBottom != 0) && (selfBottom<=toBottom)){ return ;} 83 | var ie6=!-[1,]&&!window.XMLHttpRequest; //兼容IE6 84 | 85 | $(window).on("scroll",function(){ 86 | onScroll(); 87 | }); 88 | 89 | function onScroll(){ 90 | var ScrollTop = $(window).scrollTop(); 91 | if ((ScrollTop > toTop) && (ScrollTop < endfix)) { 92 | obj.fadeIn().css({ 93 | "position": "fixed", 94 | "top": optsTop, 95 | "left": offsetLeft, 96 | "width": objWidth 97 | }); 98 | if(ie6){//IE6则使用这个样式 99 | obj.css({ 100 | "position":"absolute", 101 | "top":ScrollTop + optsTop - parentsOffset.top, 102 | "left": offsetLeft 103 | }) 104 | } 105 | placeholder.css({ 106 | 'height': outerHeight 107 | }).insertBefore(obj) 108 | } else if (ScrollTop >= endfix) { 109 | obj.css({ 110 | "position": "absolute", 111 | "top": endfix - parentsOffset.top, 112 | "left": offsetLeft - parentsOffset.left, 113 | "width": objWidth 114 | }); 115 | placeholder.css({ 116 | 'height': outerHeight 117 | }).insertBefore(obj) 118 | } else { 119 | obj.css({ 120 | "position": "static", 121 | "top": "", 122 | "left": "" 123 | }); 124 | placeholder.remove() 125 | } 126 | }; 127 | }) 128 | } 129 | })(jQuery); -------------------------------------------------------------------------------- /js/scrollfix.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | //URI:https://qdkfweb.cn/scrollfix 3 | //author:caibaojian 4 | //website:https://qdkfweb.cn 5 | //descirption:scroll and fixed some div 6 | $.fn.scrollFix = function(options) { 7 | return this.each(function() { 8 | 9 | var opts = $.extend({}, $.fn.scrollFix.defaultOptions, options); 10 | var obj = $(this), 11 | base = this, 12 | selfTop = 0, 13 | selfLeft = 0, 14 | toTop = 0, 15 | parentOffsetLeft = 0, 16 | parentOffsetTop = 0, 17 | outerHeight, 18 | outerWidth, 19 | objWidth = 0, 20 | placeholder = jQuery('
'), //创建一个jquery对象 21 | optsTop = opts.distanceTop, //定义到顶部的高度 22 | endfix = 0; //开始停止固定的位置 23 | 24 | var originalPosition; 25 | var originalOffsetTop; 26 | var originalZIndex; 27 | var lastOffsetLeft = -1; 28 | var isUnfixed = true; 29 | //如果没有找到节点,不进行处理 30 | if (obj.length <= 0) { 31 | return; 32 | } 33 | if (lastOffsetLeft == -1) { 34 | originalZIndex = obj.css('z-index'); 35 | position = obj.css('position'); 36 | originalPosition = obj.css('position'); 37 | 38 | originalOffsetTop = obj.css('top'); 39 | } 40 | 41 | var zIndex = obj.css('zIndex'); 42 | if (opts.zIndex != 0) { 43 | zIndex = opts.zIndex; 44 | } 45 | //获取相对定位或者绝对定位的父类 46 | var parents = obj.parent(); 47 | var Position = parents.css('position'); 48 | while (!/^relative|absolute$/i.test(Position)) { //检测浮动元素的父类元素定位为'relative'或者'absolute',是的话退出,否则的话,执行循环,继续寻找它的父类 49 | parents = parents.parent(); 50 | Position = parents.css('position'); 51 | if (/^body|html$/i.test(parents[0].tagName)) break; //假如父类元素的标签为body或者HTML,说明没有找到父类为以上的定位,退出循环 52 | } 53 | 54 | var ie6 = !-[1, ] && !window.XMLHttpRequest; //兼容IE6 55 | var resizeWindow = false; 56 | 57 | function resetScroll() { 58 | setUnfixed(); 59 | selfTop = obj.offset().top; //对象距离顶部高度 60 | selfLeft = obj.offset().left; //对象距离左边宽度 61 | outerHeight = obj.outerHeight(); //对象高度 62 | outerHeight = parseFloat(outerHeight) + parseFloat(obj.css('marginBottom').replace(/auto/, 0)); 63 | outerWidth = obj.outerWidth(); //对象外宽度 64 | objWidth = obj.width(); 65 | var documentHeight = $(document).height(); //文档高度 66 | var startTop = $(opts.startTop), //开始浮动固定对象 67 | startBottom = $(opts.startBottom), 68 | toBottom, //停止滚动位置距离底部的高度 69 | ScrollHeight; //对象滚动的高度 70 | 71 | //计算父类偏移值 72 | if (/^body|html$/i.test(parents[0].tagName)) { //当父类元素非body或者HTML时,说明找到了一个父类为'relative'或者'absolute'的元素,得出它的偏移高度 73 | parentOffsetTop = 0, parentOffsetLeft = 0; 74 | } else { 75 | parentOffsetLeft = parents.offset().left, parentOffsetTop = parents.offset().top; 76 | } 77 | 78 | // 计算父节点的上边到顶部距离 79 | // 如果 body 有 top 属性, 消除这些位移 80 | var bodyToTop = parseInt(jQuery('body').css('top'), 10); 81 | if (!isNaN(bodyToTop)) { 82 | optsTop += bodyToTop; 83 | } 84 | //计算停在底部的距离 85 | if (!isNaN(opts.endPos)) { 86 | toBottom = opts.endPos; 87 | } else { 88 | toBottom = parseFloat(documentHeight - $(opts.endPos).offset().top); 89 | } 90 | //计算需要滚动的高度以及停止滚动的高度 91 | ScrollHeight = parseFloat(documentHeight - toBottom - optsTop), endfix = parseFloat(ScrollHeight - outerHeight); 92 | //计算顶部的距离值 93 | if (startTop[0]) { 94 | var startTopOffset = startTop.offset(), 95 | startTopPos = startTopOffset.top; 96 | selfTop = startTopPos; 97 | } 98 | if (startBottom[0]) { 99 | var startBottomOffset = startBottom.offset(), 100 | startBottomPos = startBottomOffset.top, 101 | startBottomHeight = startBottom.outerHeight(); 102 | selfTop = parseFloat(startBottomPos + startBottomHeight); 103 | } 104 | 105 | toTop = selfTop - optsTop; 106 | toTop = (toTop > 0) ? toTop : 0; 107 | 108 | var selfBottom = documentHeight - selfTop - outerHeight; 109 | //如果滚动停在底部的值不为0,并且自身到底部的高度小于上面这个值,不执行浮动固定 110 | if ((toBottom != 0) && (selfBottom <= toBottom)) { 111 | return; 112 | } 113 | 114 | } 115 | function setUnfixed() { 116 | if (!isUnfixed) { 117 | lastOffsetLeft = -1; 118 | placeholder.css("display", "none"); 119 | obj.css({ 120 | 'z-index': originalZIndex, 121 | 'width': '', 122 | 'position': originalPosition, 123 | 'left': '', 124 | 'top': originalOffsetTop, 125 | 'margin-left': '' 126 | }); 127 | obj.removeClass('scrollfixed'); 128 | isUnfixed = true; 129 | } 130 | } 131 | 132 | function onScroll() { 133 | lastOffsetLeft = 1; 134 | var ScrollTop = $(window).scrollTop(); 135 | if (opts.bottom != -1) { 136 | ScrollTop = ScrollTop + $(window).height() - outerHeight - opts.bottom; 137 | } 138 | if (ScrollTop > toTop && (ScrollTop < endfix)) { 139 | if (ie6) { //IE6则使用这个样式 140 | obj.addClass(opts.baseClassName).css({ 141 | "z-index": zIndex, 142 | "position": "absolute", 143 | "top": opts.bottom == -1 ? ScrollTop + optsTop - parentOffsetTop : ScrollTop - parentOffsetTop, 144 | "bottom": 'auto', 145 | "left": selfLeft - parentOffsetLeft, 146 | 'width': objWidth 147 | }) 148 | } else { 149 | obj.addClass(opts.baseClassName).css({ 150 | "z-index": zIndex, 151 | "position": "fixed", 152 | "top": opts.bottom == -1 ? optsTop : '', 153 | "bottom": opts.bottom == -1 ? '' : opts.bottom, 154 | "left": selfLeft, 155 | "width": objWidth 156 | }); 157 | } 158 | placeholder.css({ 159 | 'height': outerHeight, 160 | 'width': outerWidth, 161 | 'display': 'block' 162 | }).insertBefore(obj); 163 | } else if (ScrollTop >= endfix) { 164 | obj.addClass(opts.baseClassName).css({ 165 | "z-index": zIndex, 166 | "position": "absolute", 167 | "top": endfix - parentOffsetTop + optsTop, 168 | 'bottom': '', 169 | "left": selfLeft - parentOffsetLeft, 170 | "width": objWidth 171 | }); 172 | placeholder.css({ 173 | 'height': outerHeight, 174 | 'width': outerWidth, 175 | 'display': 'block' 176 | }).insertBefore(obj) 177 | } else { 178 | obj.removeClass(opts.baseClassName).css({ 179 | "z-index": originalZIndex, 180 | "position": "static", 181 | "top": "", 182 | "bottom": "", 183 | "left": "" 184 | }); 185 | placeholder.remove() 186 | } 187 | } 188 | var Timer = 0; 189 | // if (isUnfixed) { 190 | resetScroll(); 191 | // } 192 | $(window).on("scroll", function() { 193 | if (Timer) { 194 | clearTimeout(Timer); 195 | } 196 | Timer = setTimeout(onScroll, 0); 197 | }); 198 | // 当发现调整屏幕大小时,重新执行代码 199 | $(window).on("resize", function() { 200 | if (Timer) { 201 | clearTimeout(Timer); 202 | } 203 | Timer = setTimeout(function() { 204 | isUnfixed = false; 205 | resetScroll(); 206 | onScroll(); 207 | }, 0); 208 | }); 209 | }) 210 | } 211 | $.fn.scrollFix.defaultOptions = { 212 | startTop: null, //滑到这个位置顶部时开始浮动,默认为空 213 | startBottom: null, //滑到这个位置末端开始浮动,默认为空 214 | distanceTop: 0, //固定在顶部的高度 215 | endPos: 0, //停靠在底部的位置,可以为jquery对象 216 | bottom: -1, //底部位置 217 | zIndex: 0, //z-index值 218 | baseClassName: 'scrollfixed' //开始固定时添加的类 219 | }; 220 | })(jQuery); -------------------------------------------------------------------------------- /js/scrollfix.min.js: -------------------------------------------------------------------------------- 1 | !function(a){a.fn.scrollFix=function(b){return this.each(function(){function A(){B(),f=d.offset().top,g=d.offset().left,k=d.outerHeight(),k=parseFloat(k)+parseFloat(d.css("marginBottom").replace(/auto/,0)),l=d.outerWidth(),m=d.width();var q,r,b=a(document).height(),e=a(c.startTop),n=a(c.startBottom);/^body|html$/i.test(w[0].tagName)?(j=0,i=0):(i=w.offset().left,j=w.offset().top);var s=parseInt(jQuery("body").css("top"),10);if(isNaN(s)||(o+=s),q=isNaN(c.endPos)?parseFloat(b-a(c.endPos).offset().top):c.endPos,r=parseFloat(b-q-o),p=parseFloat(r-k),e[0]){var t=e.offset(),u=t.top;f=u}if(n[0]){var v=n.offset(),x=v.top,y=n.outerHeight();f=parseFloat(x+y)}h=f-o,h=h>0?h:0;var z=b-f-k;0!=q&&q>=z}function B(){u||(t=-1,n.css("display","none"),d.css({"z-index":s,width:"",position:q,left:"",top:r,"margin-left":""}),d.removeClass("scrollfixed"),u=!0)}function C(){t=1;var b=a(window).scrollTop();-1!=c.bottom&&(b=b+a(window).height()-k-c.bottom),b>h&&p>b?(y?d.addClass(c.baseClassName).css({"z-index":v,position:"absolute",top:-1==c.bottom?b+o-j:b-j,bottom:"auto",left:g-i,width:m}):d.addClass(c.baseClassName).css({"z-index":v,position:"fixed",top:-1==c.bottom?o:"",bottom:-1==c.bottom?"":c.bottom,left:g,width:m}),n.css({height:k,width:l,display:"block"}).insertBefore(d)):b>=p?(d.addClass(c.baseClassName).css({"z-index":v,position:"absolute",top:p-j+o,bottom:"",left:g-i,width:m}),n.css({height:k,width:l,display:"block"}).insertBefore(d)):(d.removeClass(c.baseClassName).css({"z-index":s,position:"static",top:"",bottom:"",left:""}),n.remove())}var k,l,q,r,s,c=a.extend({},a.fn.scrollFix.defaultOptions,b),d=a(this),f=0,g=0,h=0,i=0,j=0,m=0,n=jQuery("
"),o=c.distanceTop,p=0,t=-1,u=!0;if(!(d.length<=0)){-1==t&&(s=d.css("z-index"),position=d.css("position"),q=d.css("position"),r=d.css("top"));var v=d.css("zIndex");0!=c.zIndex&&(v=c.zIndex);for(var w=d.parent(),x=w.css("position");!/^relative|absolute$/i.test(x)&&(w=w.parent(),x=w.css("position"),!/^body|html$/i.test(w[0].tagName)););var y=!-[1]&&!window.XMLHttpRequest,D=0;A(),a(window).on("scroll",function(){D&&clearTimeout(D),D=setTimeout(C,0)}),a(window).on("resize",function(){D&&clearTimeout(D),D=setTimeout(function(){u=!1,A(),C()},0)})}})},a.fn.scrollFix.defaultOptions={startTop:null,startBottom:null,distanceTop:0,endPos:0,bottom:-1,zIndex:0,baseClassName:"scrollfixed"}}(jQuery); -------------------------------------------------------------------------------- /reset.css: -------------------------------------------------------------------------------- 1 | @charset "utf-8"; 2 | /* ------------------------------------------------- 3 | * reset css 4 | * author: weizhi(w3plus.com) & jack cai(wpbars.com) 5 | * ------------------------------------------------- 6 | */ 7 | html{ 8 | height:100%; 9 | -webkit-text-size-adjust: 100%; 10 | -ms-text-size-adjust: 100%; 11 | -webkit-font-smoothing:antialiased; 12 | } 13 | body{ 14 | margin:0; 15 | padding:0; 16 | background-color:#fff; 17 | min-height:100%; 18 | height:auto !important; 19 | height:100%; 20 | text-align:center; 21 | color:#404040; 22 | font:12px/2 Microsoft YaHei, Helvetica,arial; 23 | } 24 | h1,h2,h3,h4,h5,h6,p,figure,form{ 25 | margin:0; 26 | font-family:Microsoft YaHei,'微软雅黑'; 27 | } 28 | table{ 29 | border-collapse:collapse; 30 | border-spacing:0; 31 | } 32 | img{ 33 | border:0; 34 | vertical-align:middle; 35 | -ms-interpolation-mode: bicubic; 36 | } 37 | a img{border:0 none;} 38 | a{ 39 | text-decoration:none; 40 | color:#29B4F0; 41 | } 42 | a:focus { 43 | outline: thin dotted; 44 | } 45 | a:hover{ 46 | color:#017CB9; 47 | text-decoration:underline; 48 | } 49 | /* hn */ 50 | h1{ 51 | font-size:16px; 52 | line-height:36px; 53 | } 54 | h2{ 55 | font-size:14px; 56 | line-height:30px; 57 | } 58 | h3{ 59 | line-height:24px; 60 | } 61 | h3,h4,h5,h6{ 62 | font-size:12px; 63 | } 64 | /* ul ol dl */ 65 | ul,li,ol{ 66 | margin: 0; 67 | padding:0; 68 | list-style: none outside none; 69 | } 70 | ul.has-style li,ol li{ 71 | margin-left:25px; 72 | } 73 | ul.has-style li{ 74 | list-style:disc; 75 | } 76 | ol li{ 77 | list-style:decimal; 78 | } 79 | ul.inline-style li{ 80 | float:left; 81 | display:inline; 82 | } 83 | dl{ 84 | margin-bottom: 18px; 85 | } 86 | dt{ 87 | font-weight: bold; 88 | } 89 | dd{ 90 | margin:0 0 0 9px; 91 | padding:0; 92 | } 93 | svg:not(:root) { 94 | overflow: hidden; 95 | } 96 | /* clearfix */ 97 | .clearfix:before, .clearfix:after { 98 | content:""; 99 | display:table; 100 | } 101 | .clearfix:after{ 102 | clear:both; 103 | overflow:hidden; 104 | } 105 | .clearfix{ 106 | zoom:1; 107 | } 108 | /* ------------------------------------------------- 109 | * form 110 | * ------------------------------------------------- 111 | */ 112 | button,input,select,textarea{ 113 | font-size:100%; 114 | font-family:tahoma; 115 | margin: 0; 116 | outline: 0 none; 117 | vertical-align: baseline; 118 | *vertical-align: middle; 119 | } 120 | textarea{ 121 | overflow: auto; 122 | vertical-align: top; 123 | resize:none; 124 | } 125 | button,input{ 126 | line-height: normal; 127 | } 128 | button, 129 | html input[type="button"], 130 | input[type="reset"], 131 | input[type="submit"], 132 | .submit-btn{ 133 | -webkit-appearance: button; 134 | cursor: pointer; 135 | *overflow: visible; 136 | } 137 | button[disabled], 138 | input[disabled], 139 | .disable{ 140 | cursor: default; 141 | } 142 | input[type="checkbox"], 143 | input[type="radio"], 144 | .form-radio, 145 | .form-checkbox{ 146 | box-sizing: border-box; 147 | padding: 0; 148 | *height: 13px; 149 | *width: 13px; 150 | } 151 | fieldset{ 152 | border: 1px solid #c0c0c0; 153 | margin: 0 2px 18px; 154 | padding: 0.35em 0.625em 0.75em; 155 | } 156 | legend{ 157 | border: 0; 158 | padding: 0; 159 | white-space: normal; 160 | *margin-left: -7px; 161 | } 162 | button::-moz-focus-inner, 163 | input::-moz-focus-inner{ 164 | border: 0; 165 | padding: 0; 166 | } 167 | /* ------------------------------------------------- 168 | * css3 & html5 169 | * ------------------------------------------------- 170 | */ 171 | /* html5 */ 172 | article,aside,details,figcaption,figure,footer,header,hgroup,nav,section,summary{ 173 | display: block; 174 | } 175 | audio,canvas,video{ 176 | display: inline-block; 177 | *display: inline; 178 | *zoom: 1; 179 | } 180 | audio:not([controls]){ 181 | display: none; 182 | height: 0; 183 | } 184 | input[type="search"]{ 185 | -webkit-appearance: textfield; 186 | -moz-box-sizing: content-box; 187 | -webkit-box-sizing: content-box; 188 | box-sizing: content-box; 189 | } 190 | :-moz-placeholder{ 191 | color: #999; 192 | } 193 | ::-webkit-input-placeholder{ 194 | color: #999; 195 | } 196 | /* ------------------------------------------------- 197 | * 非常见标签 198 | * ------------------------------------------------- 199 | */ 200 | abbr[title]{ 201 | border-bottom: 1px dotted; 202 | } 203 | b,strong{ 204 | font-weight: bold; 205 | } 206 | blockquote{ 207 | margin: 1em 40px; 208 | } 209 | dfn{ 210 | font-style: italic; 211 | } 212 | mark{ 213 | background: #ff0; 214 | color: #444; 215 | } 216 | code,kbd,pre,samp{ 217 | font-family: monospace, serif; 218 | _font-family: 'courier new', monospace; 219 | font-size: 1em; 220 | } 221 | pre{ 222 | margin: 1em 0; 223 | white-space: pre; 224 | white-space: pre-wrap; 225 | word-wrap: break-word; 226 | } 227 | q{ 228 | quotes: none; 229 | } 230 | q:before, 231 | q:after{ 232 | content: ''; 233 | content: none; 234 | } 235 | small{ 236 | font-size: 80%; 237 | } 238 | sub,sup{ 239 | font-size: 75%; 240 | line-height: 0; 241 | position: relative; 242 | vertical-align: baseline; 243 | } 244 | sup{ 245 | top: -0.5em; 246 | } 247 | sub{ 248 | bottom: -0.25em; 249 | } -------------------------------------------------------------------------------- /test/bug_demo_5.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 23 | 24 | 25 |
26 | 30 |
31 |
32 |

这只是内容,效果看侧栏

33 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
34 |
35 | 36 |
37 | 52 |
53 | 54 |
11111111
55 | 56 | 61 | 62 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /test/followSidebar.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author: mg12 [http://www.neoease.com/] 3 | * @update: 2012/12/05 4 | */ 5 | 6 | SidebarFollow = function() { 7 | 8 | this.config = { 9 | element: null, // 处理的节点 10 | distanceToTop: 0 // 节点上边到页面顶部的距离 11 | }; 12 | 13 | this.cache = { 14 | originalToTop: 0, // 原本到页面顶部的距离 15 | prevElement: null, // 上一个节点 16 | parentToTop: 0, // 父节点的上边到顶部距离 17 | placeholder: jQuery('
') // 占位节点 18 | } 19 | }; 20 | 21 | SidebarFollow.prototype = { 22 | 23 | init: function(config) { 24 | this.config = config || this.config; 25 | var _self = this; 26 | var element = jQuery(_self.config.element); 27 | 28 | // 如果没有找到节点, 不进行处理 29 | if(element.length <= 0) { 30 | return; 31 | } 32 | 33 | // 获取上一个节点 34 | var prevElement = element.prev(); 35 | while(prevElement.is(':hidden')) { 36 | prevElement = prevElement.prev(); 37 | if(prevElement.length <= 0) { 38 | break; 39 | } 40 | } 41 | _self.cache.prevElement = prevElement; 42 | 43 | // 计算父节点的上边到顶部距离 44 | var parent = element.parent(); 45 | var parentToTop = parent.offset().top; 46 | var parentBorderTop = parent.css('border-top'); 47 | var parentPaddingTop = parent.css('padding-top'); 48 | _self.cache.parentToTop = parentToTop + parentBorderTop + parentPaddingTop; 49 | 50 | // 滚动屏幕 51 | jQuery(window).scroll(function() { 52 | _self._scrollScreen({element:element, _self:_self}); 53 | }); 54 | 55 | // 改变屏幕尺寸 56 | jQuery(window).resize(function() { 57 | _self._scrollScreen({element:element, _self:_self}); 58 | }); 59 | }, 60 | 61 | /** 62 | * 修改节点位置 63 | */ 64 | _scrollScreen: function(args) { 65 | var _self = args._self; 66 | var element = args.element; 67 | var prevElement = _self.cache.prevElement; 68 | 69 | // 获得到顶部的距离 70 | var toTop = _self.config.distanceToTop; 71 | 72 | // 如果 body 有 top 属性, 消除这些位移 73 | var bodyToTop = parseInt(jQuery('body').css('top'), 10); 74 | if(!isNaN(bodyToTop)) { 75 | toTop += bodyToTop; 76 | } 77 | 78 | // 获得到顶部的绝对距离 79 | var elementToTop = element.offset().top - toTop; 80 | // console.log(elementToTop); 81 | 82 | // 如果存在上一个节点, 获得到上一个节点的距离; 否则计算到父节点顶部的距离 83 | var referenceToTop = 0; 84 | if(prevElement && prevElement.length === 1) { 85 | referenceToTop = prevElement.offset().top + prevElement.outerHeight(); 86 | } else { 87 | referenceToTop = _self.cache.parentToTop - toTop; 88 | } 89 | 90 | // 当节点进入跟随区域, 跟随滚动 91 | if(jQuery(document).scrollTop() > elementToTop) { 92 | // 添加占位节点 93 | var elementHeight = element.outerHeight(); 94 | _self.cache.placeholder.css('height', elementHeight).insertBefore(element); 95 | // 记录原位置 96 | _self.cache.originalToTop = elementToTop; 97 | console.log(_self.cache.originalToTop); 98 | console.log(elementToTop); 99 | // 修改样式 100 | element.css({ 101 | top: toTop + 'px', 102 | position: 'fixed' 103 | }); 104 | 105 | // 否则回到原位 106 | } else if(_self.cache.originalToTop > elementToTop || referenceToTop > elementToTop) { 107 | // 删除占位节点 108 | _self.cache.placeholder.remove(); 109 | // 修改样式 110 | element.css({ 111 | position: 'static' 112 | }); 113 | } 114 | } 115 | }; -------------------------------------------------------------------------------- /test/hotfix/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | scrollfix test 6 | 15 | 16 | 17 | 18 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aliquid nisi amet recusandae provident nostrum eius eum debitis facere, consequuntur tempore eos, atque eaque ab. Rem, quod, ducimus! Eveniet, laudantium laborum!
19 |
Iusto enim voluptates placeat, accusamus cumque suscipit blanditiis odit distinctio laborum doloribus dolor, alias accusantium illum numquam quis sequi consectetur dicta. Quae molestiae reprehenderit officia fugit totam culpa nesciunt voluptas.
20 |
Reiciendis placeat ducimus fugit porro sit suscipit assumenda veritatis, nam ab debitis quasi maiores animi quos sint reprehenderit, consequuntur quia! Debitis quasi, rem expedita, reprehenderit maxime voluptatum ducimus culpa impedit?
21 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quos sit laboriosam officiis nesciunt quo, error delectus aperiam eius, consequuntur quidem dolore, quod soluta minus nisi quibusdam possimus est debitis! Repellat. 22 |
23 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis inventore veritatis nam distinctio beatae totam quia esse molestias, ea, dolor, autem dignissimos amet quibusdam excepturi deleniti rerum perspiciatis? Tenetur, rerum!
24 |
Nulla perferendis et eos dicta voluptatem temporibus sed est inventore fugit tempora animi amet commodi harum mollitia possimus eaque sint praesentium laudantium architecto, laboriosam neque beatae omnis! Laudantium repudiandae, repellendus!
25 |
26 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error excepturi pariatur illo eligendi doloribus neque, dolorum, itaque, rerum libero atque quod nobis labore totam id sequi odit alias repudiandae. Doloribus.
27 |
Molestias itaque velit modi dolore exercitationem commodi unde. Ducimus deserunt, delectus asperiores praesentium quam eveniet ratione. Provident necessitatibus, quas dolorum incidunt voluptates, mollitia nulla quidem quod ullam culpa cumque sequi!
28 |
Amet odit, sunt dignissimos sapiente eligendi quisquam, qui nisi, cupiditate fuga, laboriosam exercitationem ea optio nostrum maiores iusto perferendis. Velit suscipit placeat perspiciatis ab fugit architecto iusto earum numquam nesciunt!
29 |
Doloremque possimus aspernatur accusantium aperiam, perferendis minus, beatae fugiat fugit officia aliquam praesentium amet quia dolore, nostrum laborum perspiciatis ipsa delectus error iusto adipisci ratione! Dolores non, veritatis nobis! Molestias?
30 |
Tempore ipsum culpa repellendus inventore maiores at reprehenderit saepe fuga molestias repellat suscipit odio, necessitatibus officiis facilis adipisci accusamus sit excepturi voluptatibus, laudantium, minima pariatur ducimus. Id quibusdam placeat quo.
31 |
Explicabo quaerat culpa, ducimus voluptatibus illo nostrum consequuntur pariatur non accusantium minus ad sunt omnis deleniti dicta obcaecati animi saepe odit fugiat fuga possimus quo iusto dolores! Voluptate, maxime, facilis!
32 |
Officiis, non. Id neque ratione placeat magni, magnam illo, necessitatibus ad dicta deleniti tenetur quibusdam quasi fugiat ea quas dolor inventore quia eum voluptatum tempora. Rem libero, suscipit quos nobis.
33 |
Reiciendis placeat sapiente, veritatis repellat omnis, pariatur molestiae ab sint harum! Voluptatibus, commodi nam vel nulla aperiam rem itaque laborum, quisquam debitis assumenda facere sapiente, inventore ex, aspernatur eos praesentium!
34 |
Magni consectetur nam veniam qui provident ullam tempora reiciendis quo ipsa, ex corporis quia unde alias esse aperiam maiores non ipsam tempore sit facilis, consequuntur dolore ratione id. Labore, doloribus.
35 |
Quisquam magnam beatae rem quasi distinctio quam ratione impedit, quo quod temporibus, nisi exercitationem voluptas consequatur soluta, eaque. Asperiores quaerat aliquid voluptatem quas magni doloribus, optio consectetur accusamus nemo earum?
36 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusantium quidem suscipit eum. Id numquam eaque, voluptatum illo fuga repellendus atque nesciunt quam neque itaque asperiores ab quibusdam dignissimos error accusamus.
37 |
Eveniet consequuntur, possimus saepe. Quia, vitae magni facere. Incidunt quidem velit ex odio, nostrum doloribus minima alias repellat provident labore laudantium nesciunt voluptas sint nam deleniti in consequatur fugit illum!
38 |
39 | 49 |
50 | 60 | 61 | 62 | 68 | 69 | -------------------------------------------------------------------------------- /test/hotfix/scrollfix.js: -------------------------------------------------------------------------------- 1 | // 创建一个闭包 2 | (function($) { 3 | // 插件的定义 4 | $.fn.scrollfix = function(options) { 5 | debug(this); 6 | // build main options before element iteration 7 | var opts = $.extend({}, $.fn.scrollfix.defaults, options); 8 | // iterate and reformat each matched element 9 | return this.each(function() { 10 | $this = $(this); 11 | if($this.length <= 0) return ; 12 | // build element specific options 13 | var o = $.meta ? $.extend({}, opts, $this.data()) : opts; 14 | // update element styles 15 | $this.css({ 16 | backgroundColor: o.background, 17 | color: o.foreground 18 | }); 19 | var markup = $this.html(); 20 | // call our format function 21 | markup = $.fn.scrollfix.format(markup); 22 | $this.html(markup); 23 | var parentOffset = 0 24 | 25 | 26 | 27 | 28 | 29 | function resetScroll() { 30 | var myParent = $this.parent(); 31 | var Position = myParent.css('position'); 32 | while(!/^relative|absolute$/i.test(Position)){ //检测浮动元素的父类元素定位为'relative'或者'absolute',是的话退出,否则的话,执行循环,继续寻找它的父类 33 | myParent = myParent.parent(); 34 | Position = myParent.css('position'); 35 | if(/^body|html$/i.test(myParent[0].tagName)) break; //假如父类元素的标签为body或者HTML,说明没有找到父类为以上的定位,退出循环 36 | } 37 | if(/^body|html$/i.test(myParent[0].tagName)){ //当父类元素非body或者HTML时,说明找到了一个父类为'relative'或者'absolute'的元素,得出它的偏移高度 38 | var parentOffset = {top:0,left:0}; 39 | }else{ 40 | var parentOffset = myParent.offset(); 41 | 42 | } 43 | console.log(parentOffset.top); 44 | } 45 | function onScroll(){ 46 | var ScrollTop = $(window).scrollTop(); 47 | if ((ScrollTop > toTop) && (ScrollTop < endfix)) { 48 | $this.fadeIn().css({ 49 | "position": "fixed", 50 | "top": optsTop, 51 | "left": offsetLeft, 52 | "width": objWidth 53 | }); 54 | if(ie6){//IE6则使用这个样式 55 | $this.css({ 56 | "position":"absolute", 57 | "top":ScrollTop + optsTop - parentsOffset.top, 58 | "left": offsetLeft 59 | }) 60 | } 61 | placeholder.css({ 62 | 'height': outerHeight 63 | }).insertBefore(obj) 64 | } else if (ScrollTop >= endfix) { 65 | $this.css({ 66 | "position": "absolute", 67 | "top": endfix - parentsOffset.top, 68 | "left": offsetLeft - parentsOffset.left, 69 | "width": objWidth 70 | }); 71 | placeholder.css({ 72 | 'height': outerHeight 73 | }).insertBefore(obj) 74 | } else { 75 | $this.css({ 76 | "position": "static", 77 | "top": "", 78 | "left": "" 79 | }); 80 | placeholder.remove() 81 | } 82 | }; 83 | $(window).on("scroll",function(){ 84 | resetScroll(); 85 | }) 86 | }); 87 | }; 88 | // 私有函数:debugging 89 | function debug($obj) { 90 | if (window.console && window.console.log) 91 | window.console.log('scrollfix selection count: ' + $obj.size()); 92 | }; 93 | $.fn.scrollfix.getOffset = function(evt){ 94 | var myParent = evt.parent(); 95 | var Position = myParent.css('position'); 96 | while(!/^relative|absolute$/i.test(Position)){ //检测浮动元素的父类元素定位为'relative'或者'absolute',是的话退出,否则的话,执行循环,继续寻找它的父类 97 | myParent = myParent.parent(); 98 | Position = myParent.css('position'); 99 | if(/^body|html$/i.test(myParent[0].tagName)) break; //假如父类元素的标签为body或者HTML,说明没有找到父类为以上的定位,退出循环 100 | } 101 | if(/^body|html$/i.test(myParent[0].tagName)){ //当父类元素非body或者HTML时,说明找到了一个父类为'relative'或者'absolute'的元素,得出它的偏移高度 102 | return {top:0,left:0}; 103 | }else{ 104 | return myParent.offset(); 105 | 106 | } 107 | } 108 | // 定义暴露format函数 109 | $.fn.scrollfix.format = function(txt) { 110 | return '
' + txt + '
'; 111 | }; 112 | // 插件的defaults 113 | $.fn.scrollfix.defaults = { 114 | startTop: null, //滑到这个位置顶部时开始浮动,默认为空 115 | startBottom: null, //滑到这个位置末端开始浮动,默认为空 116 | distanceTop: 0, //固定在顶部的高度 117 | endHeight: 0, //停靠在底部的高度 118 | endPos: null //停靠在底部的jquery对象 119 | }; 120 | // 闭包结束 121 | })(jQuery); -------------------------------------------------------------------------------- /test/index9.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 滚动到需要固定的地方开始固定-jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 23 | 24 | 25 |
26 |

测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确

27 |
28 |
29 | 33 |
34 |
35 |

开始浮动代码:这个是没有任何设置的,就是滚动到元素的位置开始浮动

36 |
<script type="text/javascript" src="js/scrollfix.min.js"></script>
 37 | <script type="text/javascript">
 38 | $(function(){
 39 | var fix = $(".fix");
 40 | fix.scrollFix();
 41 | })
 42 | </script>
43 |
44 |

这只是内容,效果看侧栏

45 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, culpa, officia, tempore, et vel earum delectus obcaecati ducimus at voluptates odio aliquid quis nulla illo veniam non dolorem molestias voluptas!
46 |
47 |
48 |

这只是内容,效果看侧栏

49 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
50 |
51 |
52 |

53 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
54 |
55 |
56 |

这只是内容,效果看侧栏

57 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
58 |
59 |
60 |

这只是内容,效果看侧栏

61 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
62 |
63 |
64 |

这只是内容,效果看侧栏

65 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
66 |
67 |
68 |

这是#fixFooter的位置

69 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
70 |
71 |
72 |
73 | 74 |
75 | 103 | 104 |
105 | 106 | 107 | 108 | 109 | 127 | 128 | -------------------------------------------------------------------------------- /test/scrollfix (2).js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | //URI:https://qdkfweb.cn/scrollfix 3 | //author:caibaojian 4 | //website:https://qdkfweb.cn 5 | //descirption:scroll and fixed some div 6 | $.fn.scrollFix = function(options) { 7 | return this.each(function() { 8 | var defaults = { 9 | startTop: null, //滑到这个位置顶部时开始浮动,默认为空 10 | startBottom: null, //滑到这个位置末端开始浮动,默认为空 11 | distanceTop: 0, //固定在顶部的高度 12 | endPos: 0 //停靠在底部的位置,可以为jquery对象 13 | }; 14 | 15 | var opts = $.extend({}, 16 | defaults, options), 17 | placeholder = jQuery('
'), //创建一个jquer对象 18 | 19 | obj = $(this); 20 | var ScrollTop = $(window).scrollTop(); 21 | 22 | 23 | //如果没有找到节点,不进行处理 24 | if(obj.length<=0){ 25 | return; 26 | } 27 | 28 | 29 | 30 | var ie6=!-[1,]&&!window.XMLHttpRequest; //兼容IE6 31 | 32 | function setScroll(){ 33 | var 34 | 35 | optsTop = opts.distanceTop, //定义到顶部的高度 36 | startTop = $(opts.startTop), //开始浮动固定对象 37 | startBottom = $(opts.startBottom), 38 | toBottom, //停止滚动位置距离底部的高度 39 | ScrollHeight, //对象滚动的高度 40 | endfix, //开始停止固定的位置 41 | offset = obj.offset(), 42 | offsetTop = offset.top, //对象距离顶部高度 43 | offsetLeft = offset.left, //对象距离左边宽度 44 | 45 | documentHeight = $(document).height(), //文档高度 46 | 47 | outerHeight = obj.outerHeight(), //对象高度 48 | outerHeight = parseFloat(outerHeight) + parseFloat(obj.css('marginBottom').replace(/auto/, 0)), 49 | outerWidth = obj.outerWidth(), //对象外宽度 50 | objWidth = obj.width(); 51 | var parents = obj.parent(); 52 | var Position = parents.css('position'); 53 | while(!/^relative|absolute$/i.test(Position)){ //检测浮动元素的父类元素定位为'relative'或者'absolute',是的话退出,否则的话,执行循环,继续寻找它的父类 54 | parents = parents.parent(); 55 | Position = parents.css('position'); 56 | if(/^body|html$/i.test(parents[0].tagName)) break; //假如父类元素的标签为body或者HTML,说明没有找到父类为以上的定位,退出循环 57 | } 58 | if(/^body|html$/i.test(parents[0].tagName)){ //当父类元素非body或者HTML时,说明找到了一个父类为'relative'或者'absolute'的元素,得出它的偏移高度 59 | var parentsOffset = 0; 60 | }else{ 61 | var parentsOffset = parents.offset(); 62 | 63 | } 64 | console.log(parentsOffset.left); 65 | console.log(parentsOffset.top); 66 | 67 | // 计算父节点的上边到顶部距离 68 | // 如果 body 有 top 属性, 消除这些位移 69 | var bodyToTop = parseInt(jQuery('body').css('top'), 10); 70 | console.log(typeof bodyToTop); 71 | console.log(bodyToTop); 72 | if(!isNaN(bodyToTop)) { 73 | optsTop += bodyToTop; 74 | } 75 | //计算停在底部的距离 76 | if (!isNaN(opts.endPos)) { 77 | toBottom = opts.endPos; 78 | } else { 79 | toBottom = parseFloat(documentHeight - $(opts.endPos).offset().top); 80 | } 81 | //计算需要滚动的高度以及停止滚动的高度 82 | ScrollHeight = parseFloat(documentHeight - toBottom), endfix = parseFloat(ScrollHeight - outerHeight); 83 | //计算顶部的距离值 84 | if (startTop[0]) { 85 | var startTopOffset = startTop.offset(), 86 | startTopPos = startTopOffset.top; 87 | offsetTop = startTopPos; 88 | } 89 | if (startBottom[0]) { 90 | var startBottomOffset = startBottom.offset(), 91 | startBottomPos = startBottomOffset.top, 92 | startBottomHeight = startBottom.outerHeight(); 93 | offsetTop = parseFloat(startBottomPos + startBottomHeight); 94 | } 95 | 96 | toTop = parseFloat(offsetTop - optsTop); 97 | toTop = (toTop > 0) ? toTop : 0; 98 | var selfBottom = documentHeight - offsetTop - outerHeight; 99 | //如果滚动停在底部的值不为0,并且自身到底部的高度小于上面这个值,不执行浮动固定 100 | if((toBottom != 0) && (selfBottom<=toBottom)){ return ;} 101 | 102 | 103 | if ((ScrollTop > toTop) && (ScrollTop < endfix)) { 104 | obj.fadeIn().css({ 105 | "position": "fixed", 106 | "top": optsTop, 107 | "left": offsetLeft, 108 | "width": objWidth 109 | }); 110 | if(ie6){//IE6则使用这个样式 111 | obj.css({ 112 | "position":"absolute", 113 | "top":ScrollTop + optsTop - parentsOffset.top, 114 | "left": offsetLeft 115 | }) 116 | } 117 | placeholder.css({ 118 | 'height': outerHeight 119 | }).insertBefore(obj) 120 | } else if (ScrollTop >= endfix) { 121 | obj.css({ 122 | "position": "absolute", 123 | "top": endfix - parentsOffset.top, 124 | "left": offsetLeft - parentsOffset.left, 125 | "width": objWidth 126 | }); 127 | placeholder.css({ 128 | 'height': outerHeight 129 | }).insertBefore(obj) 130 | } else { 131 | obj.css({ 132 | "position": "static", 133 | "top": "", 134 | "left": "" 135 | }); 136 | placeholder.remove() 137 | } 138 | }; 139 | $(window).on("scroll",function(){ 140 | setScroll(); 141 | // fixScroll(); 142 | }); 143 | // $(window).on("resize",function(){ 144 | // clearTimeout(timer); 145 | // timer = setTimeout(onScroll,10); 146 | // }) 147 | }) 148 | } 149 | })(jQuery); -------------------------------------------------------------------------------- /test/scrollfix.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | //URI:https://qdkfweb.cn/scrollfix 3 | //author:caibaojian 4 | //website:https://qdkfweb.cn 5 | //descirption:scroll and fixed some div 6 | $.fn.scrollFix = function(options) { 7 | return this.each(function() { 8 | var defaults = { 9 | startTop: null, //滑到这个位置顶部时开始浮动,默认为空 10 | startBottom: null, //滑到这个位置末端开始浮动,默认为空 11 | distanceTop: 0, //固定在顶部的高度 12 | endPos: 0 //停靠在底部的位置,可以为jquery对象 13 | }; 14 | var opts = $.extend({},defaults, options), 15 | _this = $(this), 16 | offset = _this.offset(), 17 | offsetLeft, 18 | offsetTop, 19 | placeholder = jQuery('
') //创建一个jquer对象 20 | ; 21 | 22 | var ie6=!-[1,]&&!window.XMLHttpRequest; //兼容IE6 23 | var timer; 24 | 25 | function onScroll(el){ 26 | var 27 | obj = el, 28 | 29 | 30 | documentHeight = $(document).height(), //文档高度 31 | optsTop = opts.distanceTop, //定义到顶部的高度 32 | outerHeight = obj.outerHeight(), //对象高度 33 | outerHeight = parseFloat(outerHeight) + parseFloat(obj.css('marginBottom').replace(/auto/, 0)), 34 | outerWidth = obj.outerWidth(), //对象外宽度 35 | objWidth = obj.width(), 36 | startTop = $(opts.startTop), //开始浮动固定对象 37 | startBottom = $(opts.startBottom), 38 | toBottom, //停止滚动位置距离底部的高度 39 | ScrollHeight, //对象滚动的高度 40 | endfix; //开始停止固定的位置 41 | console.log(offsetLeft); 42 | console.log(offsetTop); 43 | //如果没有找到节点,不进行处理 44 | if(obj.length<=0){ 45 | return; 46 | } 47 | var parents = obj.parent(); 48 | var Position = parents.css('position'); 49 | while(!/^relative|absolute$/i.test(Position)){ //检测浮动元素的父类元素定位为'relative'或者'absolute',是的话退出,否则的话,执行循环,继续寻找它的父类 50 | parents = parents.parent(); 51 | Position = parents.css('position'); 52 | if(/^body|html$/i.test(parents[0].tagName)) break; //假如父类元素的标签为body或者HTML,说明没有找到父类为以上的定位,退出循环 53 | } 54 | if(/^body|html$/i.test(parents[0].tagName)){ //当父类元素非body或者HTML时,说明找到了一个父类为'relative'或者'absolute'的元素,得出它的偏移高度 55 | var parentsOffset = 0; 56 | }else{ 57 | var parentsOffset = parents.offset(); 58 | 59 | } 60 | 61 | // 计算父节点的上边到顶部距离 62 | // 如果 body 有 top 属性, 消除这些位移 63 | var bodyToTop = parseInt(jQuery('body').css('top'), 10); 64 | if(!isNaN(bodyToTop)) { 65 | optsTop += bodyToTop; 66 | } 67 | //计算停在底部的距离 68 | if (!isNaN(opts.endPos)) { 69 | toBottom = opts.endPos; 70 | } else { 71 | toBottom = parseFloat(documentHeight - $(opts.endPos).offset().top); 72 | } 73 | //计算需要滚动的高度以及停止滚动的高度 74 | ScrollHeight = parseFloat(documentHeight - toBottom), endfix = parseFloat(ScrollHeight - outerHeight); 75 | //计算顶部的距离值 76 | if (startTop[0]) { 77 | var startTopOffset = startTop.offset(), 78 | startTopPos = startTopOffset.top; 79 | offsetTop = startTopPos; 80 | } 81 | if (startBottom[0]) { 82 | var startBottomOffset = startBottom.offset(), 83 | startBottomPos = startBottomOffset.top, 84 | startBottomHeight = startBottom.outerHeight(); 85 | offsetTop = parseFloat(startBottomPos + startBottomHeight); 86 | } 87 | 88 | toTop = parseFloat(offsetTop - optsTop); 89 | toTop = (toTop > 0) ? toTop : 0; 90 | var selfBottom = documentHeight - offsetTop - outerHeight; 91 | //如果滚动停在底部的值不为0,并且自身到底部的高度小于上面这个值,不执行浮动固定 92 | if((toBottom != 0) && (selfBottom<=toBottom)){ return ;} 93 | var ScrollTop = $(window).scrollTop(); 94 | 95 | if ((ScrollTop > toTop) && (ScrollTop < endfix)) { 96 | obj.fadeIn().css({ 97 | "position": "fixed", 98 | "top": optsTop, 99 | "left": offsetLeft, 100 | "width": objWidth 101 | }); 102 | if(ie6){//IE6则使用这个样式 103 | obj.css({ 104 | "position":"absolute", 105 | "top":ScrollTop + optsTop - parentsOffset.top, 106 | "left": offsetLeft 107 | }) 108 | } 109 | placeholder.css({ 110 | 'height': outerHeight 111 | }).insertBefore(obj) 112 | } else if (ScrollTop >= endfix) { 113 | obj.css({ 114 | "position": "absolute", 115 | "top": endfix - parentsOffset.top, 116 | "left": offsetLeft - parentsOffset.left, 117 | "width": objWidth 118 | }); 119 | placeholder.css({ 120 | 'height': outerHeight 121 | }).insertBefore(obj) 122 | } else { 123 | obj.css({ 124 | "position": "static", 125 | "top": "", 126 | "left": "" 127 | }); 128 | placeholder.remove() 129 | } 130 | }; 131 | $(window).on("scroll",function(){ 132 | 133 | offsetTop = offset.top, //对象距离顶部高度 134 | offsetLeft = offset.left; //对象距离左边宽度 135 | onScroll(_this); 136 | }); 137 | $(window).on("resize",function(){ 138 | offset = _this.offset(), 139 | offsetTop = offset.top, //对象距离顶部高度 140 | offsetLeft = offset.left; //对象距离左边宽度 141 | onScroll(_this); 142 | 143 | }); 144 | 145 | }) 146 | } 147 | })(jQuery); -------------------------------------------------------------------------------- /test/scrollfix2.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | //URI:https://qdkfweb.cn/scrollfix 3 | //author:caibaojian 4 | //website:https://qdkfweb.cn 5 | //descirption:scroll and fixed some div 6 | $.fn.scrollFix = function(options) { 7 | return this.each(function() { 8 | var defaults = { 9 | startTop: null, //滑到这个位置顶部时开始浮动,默认为空 10 | startBottom: null, //滑到这个位置末端开始浮动,默认为空 11 | distanceTop: 0, //固定在顶部的高度 12 | endPos: 0 //停靠在底部的位置,可以为jquery对象 13 | }; 14 | var opts = $.extend({}, 15 | defaults, options), 16 | obj = $(this), 17 | offset = obj.offset(), 18 | offsetTop = offset.top, //对象距离顶部高度 19 | offsetLeft = offset.left, //对象距离左边宽度 20 | placeholder = jQuery('
'), //创建一个jquer对象 21 | documentHeight = $(document).height(), //文档高度 22 | optsTop = opts.distanceTop, //定义到顶部的高度 23 | outerHeight = obj.outerHeight(), //对象高度 24 | outerHeight = parseFloat(outerHeight) + parseFloat(obj.css('marginBottom').replace(/auto/, 0)), 25 | outerWidth = obj.outerWidth(), //对象外宽度 26 | objWidth = obj.width(), 27 | startTop = $(opts.startTop), //开始浮动固定对象 28 | startBottom = $(opts.startBottom), 29 | toBottom, //停止滚动位置距离底部的高度 30 | ScrollHeight, //对象滚动的高度 31 | endfix; //开始停止固定的位置 32 | 33 | //如果没有找到节点,不进行处理 34 | if(obj.length<=0){ 35 | return; 36 | } 37 | var parents = obj.parent(); 38 | var Position = parents.css('position'); 39 | while(!/^relative|absolute$/i.test(Position)){ //检测浮动元素的父类元素定位为'relative'或者'absolute',是的话退出,否则的话,执行循环,继续寻找它的父类 40 | parents = parents.parent(); 41 | Position = parents.css('position'); 42 | if(/^body|html$/i.test(parents[0].tagName)) break; //假如父类元素的标签为body或者HTML,说明没有找到父类为以上的定位,退出循环 43 | } 44 | if(/^body|html$/i.test(parents[0].tagName)){ //当父类元素非body或者HTML时,说明找到了一个父类为'relative'或者'absolute'的元素,得出它的偏移高度 45 | var parentsOffset = 0; 46 | }else{ 47 | var parentsOffset = parents.offset(); 48 | 49 | } 50 | 51 | // 计算父节点的上边到顶部距离 52 | // 如果 body 有 top 属性, 消除这些位移 53 | var bodyToTop = parseInt(jQuery('body').css('top'), 10); 54 | console.log(typeof bodyToTop); 55 | console.log(bodyToTop); 56 | if(!isNaN(bodyToTop)) { 57 | optsTop += bodyToTop; 58 | } 59 | //计算停在底部的距离 60 | if (!isNaN(opts.endPos)) { 61 | toBottom = opts.endPos; 62 | } else { 63 | toBottom = parseFloat(documentHeight - $(opts.endPos).offset().top); 64 | } 65 | //计算需要滚动的高度以及停止滚动的高度 66 | ScrollHeight = parseFloat(documentHeight - toBottom), endfix = parseFloat(ScrollHeight - outerHeight); 67 | //计算顶部的距离值 68 | if (startTop[0]) { 69 | var startTopOffset = startTop.offset(), 70 | startTopPos = startTopOffset.top; 71 | offsetTop = startTopPos; 72 | } 73 | if (startBottom[0]) { 74 | var startBottomOffset = startBottom.offset(), 75 | startBottomPos = startBottomOffset.top, 76 | startBottomHeight = startBottom.outerHeight(); 77 | offsetTop = parseFloat(startBottomPos + startBottomHeight); 78 | } 79 | 80 | toTop = parseFloat(offsetTop - optsTop); 81 | toTop = (toTop > 0) ? toTop : 0; 82 | var selfBottom = documentHeight - offsetTop - outerHeight; 83 | //如果滚动停在底部的值不为0,并且自身到底部的高度小于上面这个值,不执行浮动固定 84 | if((toBottom != 0) && (selfBottom<=toBottom)){ return ;} 85 | var ie6=!-[1,]&&!window.XMLHttpRequest; //兼容IE6 86 | var timer; 87 | $(window).on("scroll",function(){ 88 | clearTimeout(timer); 89 | timer = setTimeout(onScroll,10); 90 | }); 91 | // $(window).on("resize",function(){ 92 | // clearTimeout(timer); 93 | // timer = setTimeout(onScroll,10); 94 | // }) 95 | function onScroll(){ 96 | var ScrollTop = $(window).scrollTop(); 97 | if ((ScrollTop > toTop) && (ScrollTop < endfix)) { 98 | obj.fadeIn().css({ 99 | "position": "fixed", 100 | "top": optsTop, 101 | "left": offsetLeft, 102 | "width": objWidth 103 | }); 104 | if(ie6){//IE6则使用这个样式 105 | obj.css({ 106 | "position":"absolute", 107 | "top":ScrollTop + optsTop - parentsOffset.top, 108 | "left": offsetLeft 109 | }) 110 | } 111 | placeholder.css({ 112 | 'height': outerHeight 113 | }).insertBefore(obj) 114 | } else if (ScrollTop >= endfix) { 115 | obj.css({ 116 | "position": "absolute", 117 | "top": endfix - parentsOffset.top, 118 | "left": offsetLeft - parentsOffset.left, 119 | "width": objWidth 120 | }); 121 | placeholder.css({ 122 | 'height': outerHeight 123 | }).insertBefore(obj) 124 | } else { 125 | obj.css({ 126 | "position": "static", 127 | "top": "", 128 | "left": "" 129 | }); 130 | placeholder.remove() 131 | } 132 | }; 133 | }) 134 | } 135 | })(jQuery); -------------------------------------------------------------------------------- /test/test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | offset和position测试1 7 | 8 | 29 | 43 | 44 | 45 | 46 |
47 | 父容器的position是默认值,也就是static,子容器的position为默认值,也是static,这个时候,offset和position值相同


48 |
49 |
50 |
51 |
52 |
53 | 父容器的position是相对定位,也就是ralative,子容器的position为默认值,也是static,这个时候,offset和position值不同 54 |
55 |
56 |
57 |
58 |
59 | 父容器的position是绝对定位,也就是absolute,子容器的position为默认值,也是static,这个时候,offset和position值不同 60 |
61 |
62 |
63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /test/test2.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 滚动到需要固定的地方开始固定-jquery滚动固定插件:scrollfix演示-前端博客 6 | 7 | 8 | 9 | 10 | 11 | 23 | 24 | 25 |
26 |

测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确测试当position:relative的位置不是body的时候的结果是否正确

27 |
28 |
29 | 33 |
34 |
35 |

开始浮动代码:这个是没有任何设置的,就是滚动到元素的位置开始浮动

36 |
<script type="text/javascript" src="js/scrollfix.min.js"></script>
 37 | <script type="text/javascript">
 38 | $(function(){
 39 | var fix = $(".fix");
 40 | fix.scrollFix();
 41 | })
 42 | </script>
43 |
44 |

这只是内容,效果看侧栏

45 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, culpa, officia, tempore, et vel earum delectus obcaecati ducimus at voluptates odio aliquid quis nulla illo veniam non dolorem molestias voluptas!
46 |
47 |
48 |

这只是内容,效果看侧栏

49 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
50 |
51 |
52 |

53 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
54 |
55 |
56 |

这只是内容,效果看侧栏

57 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
58 |
59 |
60 |

这只是内容,效果看侧栏

61 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
62 |
63 |
64 |

这只是内容,效果看侧栏

65 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
66 |
67 |
68 |

这是#fixFooter的位置

69 |
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nesciunt, consequatur, dolorem eaque culpa ut iste tempora molestias repudiandae magni quo excepturi sapiente nisi reprehenderit omnis ea! Libero aspernatur fugit beatae?
70 |
71 |
72 |
73 | 74 |
75 | 103 | 104 |
105 | 106 | 107 | 108 | 109 | 117 | 118 | --------------------------------------------------------------------------------