1. 程式人生 > >左側 隨著頁面滾動固定 fixed. scroll .scrollTop

左側 隨著頁面滾動固定 fixed. scroll .scrollTop

style itl text htm width offset echo 代碼 images

1.圖片.

技術分享

要求:隨著頁面滾動 . 左側應該頂著 瀏覽器頂部, 向上回滾, 就恢復原狀.

2. 代碼:

html

  1     <div class="all ">
  2 
  3         <!-- 頭部: 背景 -->
  4         <div class="all_head">
  5 
  6         </div>
  7 
  8         <div class="all_logout" onclick="logout()">
  9             退出
 10         </
div> 11 12 13 14 <!-- 包裹: 左邊 和 右邊 --> 15 <div class="all_wrap floatfix"> 16 17 <!-- 左邊 --> 18 <div class="all_left"> 19 20 <!-- 頭部_頭像區域 --> 21 <div class="all_top floatfix"> 22
<img class="all_top_img" src="<?php echo $user[‘userImg‘]; ?>" alt="用戶頭像"> 23 <div class="all_top_msg"> 24 <span class="all_top_msg_name"> <?php echo $user[‘userName‘]; ?></span> 25 <
span class="caret" style="border-bottom-color: #fff; border-top-color: #fff; margin-left: 10px;"></span> 26 </div> 27 28 29 </div> 30 31 32 <!-- 導航 --> 33 34 35 <ul class="ul_1"> 36 37 <?php foreach ($nav as $k1 => $model1): ?> 38 <li class="li_1"> 39 <div class="li_1_content " onclick="getFirst(this, <?php echo $model1->id; ?>)"> 40 41 <?php if ($model1->hasStudy == 0): ?> 42 <div class="li_1_text "><?php echo $model1->title; ?></div> 43 <?php else: ?> 44 <div class="li_1_text hasStudy"><?php echo $model1->title; ?></div> 45 <?php endif ?> 46 47 </div> 48 49 <?php if (count($model1->child) > 0): ?> 50 <ul class="ul_2 hide"> 51 52 <?php foreach ($model1->child as $k2 => $model2): ?> 53 <li class="li_2" > 54 <div class="li_2_content" onclick="getSecond(this, <?php echo $model2->id; ?>, event)"> 55 <div class="li_2_text"> 56 <?php echo $model2->title; ?> 57 </div> 58 </div> 59 60 61 <?php if (count($model2->child) > 0): ?> 62 <ul class="ul_3 hide"> 63 64 <?php foreach ($model2->child as $k3 => $model3): ?> 65 <li class="li_3" > 66 <div class="li_3_content" onclick="getThird(this, <?php echo $model3->id; ?>, event)"> 67 <div class="li_3_text"><?php echo $model3->title; ?></div> 68 </div> 69 </li> 70 <?php endforeach ?> 71 72 73 </ul> 74 <?php endif ?> 75 76 </li> 77 <?php endforeach ?> 78 79 </ul> 80 <?php endif ?> 81 82 83 </li> 84 <?php endforeach ?> 85 86 87 </ul> 88 89 90 91 92 93 94 95 96 97 </div> 98 99 <!-- 右邊 --> 100 <div class="all_right"> 101 102 <div class="all_right_blue"> 103 104 <div class="all_right_bg all_right_bg_img"> 105 106 107 108 109 110 </div> 111 112 </div> 113 114 </div> 115 </div> 116 117 118 </div>

js:

 1             //滾動----固定效果
 2             var maxScrollTop = 342;
 3             var pflag = false;
 4             $(window).scroll( maxScrollTop, function(event){
 5                 var $me = $(this);
 6                 console.log($me.scrollTop());
 7                 // console.log($(".all_right_blue").offset());
 8                 // console.log($(".all_right_blue").position());
 9 
10                 if( $me.scrollTop() > event.data ){
11 
12                     if(pflag == false){
13                         $(".all_left").css("position", "fixed");
14                         $(".all_left").css("top", "0px");
15                         flag = true;
16                         // console.log(‘1111‘);
17 
18                     }
19 
20                     // $me.scrollTop( 0 );
21                 }else{
22                     $(".all_left").css("position", "static");
23                     pflag == false;
24                     // $("#xtest").css("top", $me.scrollTop());
25                 }
26             } );

1 <ul class="ul_1" style="max-height:600px; overflow:auto;">

3 .效果:

技術分享

向下滾動: 左邊固定在頂部

技術分享

向上滾動: 左側恢復

技術分享

左側 隨著頁面滾動固定 fixed. scroll .scrollTop