Home > Web Front-end > JS Tutorial > body text

JQuery implements the following advertising effects on the right side of the web page_jquery

WBOY
Release: 2016-05-16 15:19:35
Original
1475 people have browsed it

Method 1:

<script type="text/javascript">// <![CDATA[ 
$.fn.smartFloat = function() { 
  var position = function(element) { 
    var top = element.position().top, pos = element.css("position"); 
    $(window).scroll(function() { 
      var scrolls = $(this).scrollTop(); 
      if (scrolls > top) { 
        if (window.XMLHttpRequest) { 
          element.css({ 
            position: "fixed", 
            top: "10px" 
          }); 
        } else { 
          element.css({ 
            top: scrolls 
          }); 
        } 
      }else { 
        element.css({ 
          position: pos, 
          top: top 
        }); 
      } 
    }); 
  }; 
  return $(this).each(function() { 
    position($(this)); 
  }); 
}; 
 
//绑定 
$("#float").smartFloat(); 
// ]]></script> 
Copy after login

Method 2:

/*页面智能层浮动*/ 
jQuery(document).ready(function($){ 
  var $sidebar = $(".float"), 
  $window = $(window), 
  offset = $sidebar.offset(), 
  topPadding = 20; 
  $window.scroll(function() { 
    if ($window.scrollTop() > offset.top) { 
      $sidebar.stop().animate({ 
        marginTop: $window.scrollTop() - offset.top + topPadding 
      }); 
    } else { 
      $sidebar.stop().animate({ 
        marginTop: 0 
      }); 
    } 
  }); 
}); 
Copy after login

HTML

<div id="float" class="float"> 
<h3>推荐</h3> 
广告代码 
</div> 
Copy after login

The above two methods can achieve the following advertising effects on the right side of the page. I hope it can be helpful to everyone.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template