2013-01-05 45 views
0

我使用了一些由Tats在JSFiddle上发布的代码。它适用于JSFiddle:http://jsfiddle.net/FWWEn/397/ 但不在我的html页面上。我在这些脚本标记之间插入的功能:平滑选框不工作

script type='text/javascript' src='http://code.jquery.com/jquery.min.js' 

之前/html标签插入它在我的.asp文件的末尾。 我插入的h1标签没有工作。为什么不?

<body> 
<h1 align="center" bgcolor="#FFFFCC" width="800" class="style33" > Hours next week are 8:30am - 6:30pm. </h1> 
</body> 

<script type='text/javascript' src='http://code.jquery.com/jquery.min.js'> 
function($) { 
    $.fn.textWidth = function(){ 
     var calc = '<span style="display:none">' + $(this).text() + '</span>'; 
     $('body').append(calc); 
     var width = $('body').find('span:last').width(); 
     $('body').find('span:last').remove(); 
     return width; 
    }; 
    $.fn.marquee = function(args) { 
     var that = $(this); 
     var textWidth = that.textWidth(), 
      offset = that.width(), 
      width = offset, 
      css = { 
       'text-indent' : that.css('text-indent'), 
       'overflow' : that.css('overflow'), 
       'white-space' : that.css('white-space') 
      }, 
      marqueeCss = { 
       'text-indent' : width, 
       'overflow' : 'hidden', 
       'white-space' : 'nowrap' 
      }, 
      args = $.extend(true, { count: -1, speed: 1e1, leftToRight: false }, args), 
      i = 0, 
      stop = textWidth*-1, 
      dfd = $.Deferred(); 

     function go() { 
      if(!that.length) return dfd.reject(); 
      if(width == stop) { 
       i++; 
       if(i == args.count) { 
        that.css(css); 
        return dfd.resolve(); 
       } 
       if(args.leftToRight) { 
        width = textWidth*-1; 
       } else { 
        width = offset; 
       } 
      } 
      that.css('text-indent', width + 'px'); 
      if(args.leftToRight) { 
       width++; 
      } else { 
       width--; 
      } 
      setTimeout(go, args.speed); 
     }; 
     if(args.leftToRight) { 
      width = textWidth*-1; 
      width++; 
      stop = offset; 
     } else { 
      width--;    
     } 
     that.css(marqueeCss); 
     go(); 
     return dfd.promise(); 
    }; 
      $('h1').marquee(); 
})(jQuery); 
</script> 

回答

0

使用脚本标记用设置好的,除非有一个源问题将无法解释它里面的代码src属性。

所以只是包括jQuery的是这样的:

<script type='text/javascript' src='http://code.jquery.com/jquery.min.js'></script> 

然后用一个新的脚本,包括您的代码:

<script> 
function($) { 
    $.fn.textWidth = function(){ 
     var calc = '<span style="display:none">' + $(this).text() + '</span>'; 
     $('body').append(calc); 
     var width = $('body').find('span:last').width(); 
     $('body').find('span:last').remove(); 
     return width; 
    }; 

    $.fn.marquee = function(args) { 
     var that = $(this); 
     var textWidth = that.textWidth(), 
      offset = that.width(), 
      width = offset, 
      css = { 
       'text-indent' : that.css('text-indent'), 
       'overflow' : that.css('overflow'), 
       'white-space' : that.css('white-space') 
      }, 
      marqueeCss = { 
       'text-indent' : width, 
       'overflow' : 'hidden', 
       'white-space' : 'nowrap' 
      }, 
      args = $.extend(true, { count: -1, speed: 1e1, leftToRight: false }, args), 
      i = 0, 
      stop = textWidth*-1, 
      dfd = $.Deferred(); 

     function go() { 
      if(!that.length) return dfd.reject(); 
      if(width == stop) { 
       i++; 
       if(i == args.count) { 
        that.css(css); 
        return dfd.resolve(); 
       } 
       if(args.leftToRight) { 
        width = textWidth*-1; 
       } else { 
        width = offset; 
       } 
      } 
      that.css('text-indent', width + 'px'); 
      if(args.leftToRight) { 
       width++; 
      } else { 
       width--; 
      } 
      setTimeout(go, args.speed); 
     }; 
     if(args.leftToRight) { 
      width = textWidth*-1; 
      width++; 
      stop = offset; 
     } else { 
      width--;    
     } 
     that.css(marqueeCss); 
     go(); 
     return dfd.promise(); 
    }; 
      $('h1').marquee(); 
})(jQuery); 
</script> 
+0

感谢烤。工作完美! – user1951308

+0

选框贯穿屏幕。如何编辑jquery使文本居中并使用选定的颜色背景将宽度限制为1000? – user1951308