javascript - jQuery .css property not working on scroll function -


i learning jquery implementing properties. having difficulties when i'm trying make div stick top of page when scrolled to. html div:

<div class="col span_1_of_2 poll_div" id="poll-div">                     <p>poll of week</p>                     <p>best pakistani singer?</p>                     <table>                         <tr>                             <td width="100%">                                 <input type="submit" id="op1" onclick="load(this.id);" class="options option1" value="option # 1"/>                             </td>                             <td class="numb">                                 <div id="votes1"><?php $conn = mysql_connect('localhost', 'root', '');                                                          mysql_select_db('poll');                                                         $row= mysql_fetch_assoc(mysql_query("select * `polls` 1"));                                                         echo $row['option1'] . "/" . $row['total'] ?></div>                             </td>                         </tr>                          <tr>                             <td width="100%">                                 <input type="submit" id="op2" onclick="load(this.id);" class="options option2" value="option # 1"/>                             </td>                             <td class="numb">                                 <div id="votes2"><?php echo $row['option2'] . "/" . $row['total'] ?></div>                             </td>                         </tr>                          <tr>                             <td width="100%">                                 <input type="submit" id="op3" onclick="load(this.id);" class="options option3" value="option # 1"/>                             </td>                             <td class="numb">                                 <div id="votes3"><?php echo $row['option3'] . "/" . $row['total'] ?></div>                             </td>                         </tr>                     </table>                 </div> <!--poll--> 

edit css:

background-color: white; position: fixed; width: 25%; float: right; margin-right: 2%; margin-top: 3.5%; border: 1px solid #ccc; box-shadow: 2px 2px 2px 2px #ccc; padding-bottom: 30px;  

and jquery:

$(document).ready(function(){      $(window).scroll(function(){         if($(this).scrolltop() > 280 && $("#poll_div").css('position') == 'relative'){             $("#poll_div").css("position", "fixed");         }          if($(this).scrolltop() < 400 && $("#poll_div").css('position') == 'fixed'){             $("#poll_div").css({'position': 'static'});         }     }); }); 

if place line window.alert("hello"); instead of css property change in jquery, works fine not css property line.

add position fixed

you can add position:fixed; in style , accomplish need

.poll_div{     background-color: white;     position: fixed;//removed position relative     width: 25%;     float: right;     margin-right: 2%;     margin-top: 3.5%;     border: 1px solid #ccc;     box-shadow: 2px 2px 2px 2px #ccc;     padding-bottom: 30px;     } 

example

updated demo


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -