JQuery If 条件(特定于内容滑块)

发布于 2024-10-01 02:43:39 字数 373 浏览 2 评论 0原文

我是 JQuery 的新手,我有一个关于 IF-THEN-ELSE 分支的具体问题。 对我来说最大的问题是它的语法(我不擅长Javascript)。如果有人可以将伪代码“翻译”为 JQuery(或 Javascript)有效代码,这将对我有所帮助。

伪代码:

IF "#Contentshowroom" css "left" is NOT > 1960px

然后 点击“#Forwardbutton”即可 animate "#Contentshowroom" css "left" =+980px

ELSE 您无法点击“#Forwardbutton”

I am new at JQuery and I have a specific question about the IF-THEN-ELSE fork.
The big problem for me is the syntax of this (I suck at Javascript). It would help me if anyone can "translate" the pseudo code into a JQuery (or Javascript) valide code.

The pseudo code:

IF "#Contentshowroom" css "left" is NOT > 1960px

THEN
On Click "#Forwardbutton" DO
animate "#Contentshowroom" css "left" =+980px

ELSE You can not click on the "#Forwardbutton"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最笨的告白 2024-10-08 02:43:39

将 if() 语句放置在 #Forwardbutton 的点击处理程序中,以测试 #Contentshowroom 的左侧位置。

如果您使用的是 jQuery:

$('#Forwardbutton').click(function() {
    var $Content = $('#Contentshowroom');
    if( $Content.offset().left <= 1960 ) {
        $Content.animate({ left: '+= 980' });
    }
});

那么现在当您单击 Forwardbutton 时,它将检查 Contentshowroom 的左侧 .offset() 位置查看它是否小于或等于1960px。如果是这样,它将为左侧位置添加额外的 980px 动画。

jQuery 的 .offset() 方法 为您提供 top/left< /code> 相对于 body 的位置。如果您希望它相对于其父容器,请使用 jQuery 的 .position() 方法< /a>.

Place the if() statement in the click handler for #Forwardbutton to test the left position of #Contentshowroom.

If you're using jQuery:

$('#Forwardbutton').click(function() {
    var $Content = $('#Contentshowroom');
    if( $Content.offset().left <= 1960 ) {
        $Content.animate({ left: '+= 980' });
    }
});

So now when you click the Forwardbutton, it will check the left .offset() position of the Contentshowroom to see if it is less than or equal to 1960px. And if so, it will animate the left position an additional 980px.

jQuery's .offset() method gives you the top/left positions relative to the body. If you want it relative to its parent container, then use jQuery's .position() method.

何时共饮酒 2024-10-08 02:43:39

点击文档
动画文档
offset doc

$("#Forwardbutton").click( function( e ){

   // lookup is safe, no noticable performance cost.
   // though a reference makes it more losely coupled.
   // I'll leave it at your discretion.
   var target = $("#Contentshowroom")
   // NOTE: the offset parent should have position relative or absolute.
   , leftPos = target.offset().left; 

   if ( leftPos < 1960 ) {

      target.animate({
         left : leftPos + 980
      }); // see docs to tweak animation

   } // else do nothing.

} );

也可以使用 e.preventDefault(); ,但不要'如果不需要的话,如果您向按钮添加更多侦听器并发现它们不起作用,那么您会很头痛。

click doc
animate doc
offset doc

$("#Forwardbutton").click( function( e ){

   // lookup is safe, no noticable performance cost.
   // though a reference makes it more losely coupled.
   // I'll leave it at your discretion.
   var target = $("#Contentshowroom")
   // NOTE: the offset parent should have position relative or absolute.
   , leftPos = target.offset().left; 

   if ( leftPos < 1960 ) {

      target.animate({
         left : leftPos + 980
      }); // see docs to tweak animation

   } // else do nothing.

} );

Could also use e.preventDefault(); , but don't if it's not needed, it will safe you headaches if you add more listeners to your buttons and find out they're not working.

盗琴音 2024-10-08 02:43:39
// first store contentShowroom and it's left property to save getting > 1
var contentShowroom = $('#Contentshowroom');
var showroomLeft = contentShowroom.css('left');
var forwardButton = $('#Forwardbutton');

if (showroomLeft <= 1960){
  forwardButton.click(function(){
    contentShowroom.animate({left: showroomLeft + 980);
  }
}
else {
  forwardButton.unbind('click');
}
// first store contentShowroom and it's left property to save getting > 1
var contentShowroom = $('#Contentshowroom');
var showroomLeft = contentShowroom.css('left');
var forwardButton = $('#Forwardbutton');

if (showroomLeft <= 1960){
  forwardButton.click(function(){
    contentShowroom.animate({left: showroomLeft + 980);
  }
}
else {
  forwardButton.unbind('click');
}
哆兒滾 2024-10-08 02:43:39

如果要在开始时运行一次那么

if ( $('#Contentshowroom').offset().left > 1960 )
{
  $('#Forwardbutton').click( function(){
    $('#Contentshowroom').animate({left:'+=980'});
  } );
}
else
{
  // if the #Contentshowroom is a link then 
  $('#Contentshowroom').removeAttr('href');
  // if the #Contentshowroom is a button then 
  // $('#Contentshowroom').attr('disabled',true);
}

if this is to be run once at the beginning then

if ( $('#Contentshowroom').offset().left > 1960 )
{
  $('#Forwardbutton').click( function(){
    $('#Contentshowroom').animate({left:'+=980'});
  } );
}
else
{
  // if the #Contentshowroom is a link then 
  $('#Contentshowroom').removeAttr('href');
  // if the #Contentshowroom is a button then 
  // $('#Contentshowroom').attr('disabled',true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文