Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Position incorrect when on fixed elements #13

Closed
MattShrider opened this issue Dec 12, 2014 · 16 comments
Closed

Position incorrect when on fixed elements #13

MattShrider opened this issue Dec 12, 2014 · 16 comments

Comments

@MattShrider
Copy link

JSbin with issuee:
http://jsbin.com/xirukapuho/1/edit?html,css,js,output

When the dropdown is on a fixed element, floating somewhere below the top of the page, the dropdown position is calculated incorrectly. I think this is due to the fact that the position is calculated relative to the document, but the absolute style is relative to the parent.

This could solved by finding the directive element's offset from the top of the page (and left) and subtracting from the position value.

MattShrider added a commit to MattShrider/MassAutocomplete that referenced this issue Dec 12, 2014
@hakib
Copy link
Owner

hakib commented Dec 13, 2014

Hey @MattShrider, I just took a look at your proposed PR.

The specific problem you encountered exists only if the container is in a fixed positioned element. Placing the mass-autocomplete container out of the fixed element make the positioning work fine as it is today (look here ). Trying the same example with your dev version will cause problems when scrolling (see here ).

So this reduces our problem to situations where you have the AC container within a fixed positioned container.

I've set up an example here with all situations. As you can see the situation where the AC-container is within a fixed positioned element does not position correctly. I though of setting the AC container to relative but then any fixed input within that container will not position properly (I also assume that this is the common use case).

I know that other libraries solve this by appending the menu to the element's container. I would like to avoid that if possible. Do you have any suggestions ?

Thanks, Haki.

@elifio
Copy link

elifio commented Jan 11, 2015

I'm Using ngDialog a ngmodule to open modals windows
and displays also in the wrong position.

It is possible to solve the problem by changing the position in the script

You can see in the plunker

http://plnkr.co/edit/4aDGuv?p=preview

@hakib
Copy link
Owner

hakib commented Jan 12, 2015

Hey @elifio,
This is the problem we discussed above - How to properly position an element on a fixed container.

I'm a bit time constrained at the moment so if anyone wants to take a crack at this i would appreciate it.

Haki.

@psdcoder
Copy link

Hi! I think we shouldn't calculate position of dropdown dynamically.
I forked mentioned above plunker and added styles which fix that problem: http://plnkr.co/edit/3rfOkq4D46mzwB5FXo53?p=preview

/* fix styles */
[mass-autocomplete] {
  position: relative;
}

.ac-container {
  position: absolute;
  top: 100% !important;
  left: 0 !important;
  width: 100% !important;
}

What do you think about that?

@hakib
Copy link
Owner

hakib commented Jan 24, 2015

Hey @psdcoder , This might solve @elifio problem but it wont solve the fixed positioning problem discussed above, look here.

You cannot use relative positioning on the AC container unless you append it to the actual element.

@kperwel
Copy link

kperwel commented Feb 4, 2015

Thanks @psdcoder. Imo it's perfect solution. I don't see reason to use JS for positioning, when there is still additional container required. There should be information in documentation, that container have to be exactly in size of input.

@hakib, How about this one?
http://jsbin.com/megodehibi/18/

I know, a lot more (mass-autocomplete) directives, but more bulletproof solution, and less js calculations.

@asciant
Copy link

asciant commented Apr 28, 2015

@hakib Thanks for taking the time to explain this one, I moved the <div mass-autocomplete></div> a little further up the tree and it started rendering in the correct position.

Cheers

@jbarros35
Copy link

Tried everything didnt worked as expected.

´

´
´ ´

What's New






<textarea class="form-control vresize" rows="5" placeholder="Update your status, the first phrase before break will be your title post." ng-model="description" required my-maxlength="1024"></textarea>

   <div ng-controller="mainCtrl" class="form-group">
       <div mass-autocomplete>
           <input ng-model="dirty.value" mass-autocomplete-item="autocomplete_options">
       </div>
   </div>
    <!--input type="text" class="form-control vresize" auto-complete ui-items="names"
           placeholder="Music Categories, band names, music name etc (ex:rock,indie,pop) No space allowed."
           ng-pattern="/^[a-zA-Z0-9,]*$/" ng-model="categories" required ng-maxlength="155"/-->

  <button class="btn btn-success pull-right" type="submit" ng-disabled="postForm.$invalid">Post</button>

 <ul class="list-inline">
  <li><a href=""></a></li>
  <li><a href=""></a></li>
  <li><a href=""></a></li></ul>

</form>
´

ps: its bootstrap classes.

@albogdano
Copy link

I'm using Foundation with AngularJS 1.4.8 and had similar issues with positioning but I wasn't attaching to a fixed element. The CSS suggested by @psdcoder helped me fix the issue. Perhaps this could be added to the docs in the future. Thanks for the great plugin!

@imdreamrunner
Copy link

Thanks @psdcoder your code works for me.

@thethakuri
Copy link

Removing following lines when used inside a modal worked for me:

container.style.top = rect.top + rect.height + scrollTop + 'px'; 
container.style.left = rect.left + scrollLeft + 'px'; 

@hakib
Copy link
Owner

hakib commented Jul 23, 2016

I just pushed v0.3.0 with a way of overriding the default positioning function. By overriding the function you could set the container to be fixed positioned and made all the necessary adjustments to the ac container.

@hakib hakib closed this as completed Jul 23, 2016
@abourakba
Copy link

Hello @hakib
Could you explain how it works now ?
The documentation states that I can use position_autocomplete(container, target)
I tried to add a position_autocomplete function in my autocomplete_options but it does not work.
$scope.autocomplete_options = { suggest: $scope.suggest_state, on_select : $scope.suggest_selecthandler, on_error : function(){ Notification.error("Auto Complete Fail !"); }, position_autocomplete : function(container, target){ console.log("position_autocomplete ") } };

Best Regards
Abdou

@hakib
Copy link
Owner

hakib commented Sep 13, 2017

Hey @abourakba,

The position_autocomplete settings is done via the directive provider.

In you'r main app inject the provder

angular.module('app', [
    'ngSanitize',
    'MassAutoComplete',
])

.run(['massAutoCompleteConfigProvider', function(massAutoCompleteConfigProvider) { 

    massAutoCompleteConfigProvide.position_autocomplete(container, target) {
        // Your code.
    }
}]);

@abourakba
Copy link

Hello @hakib
I used a cheap solution by adding a css class to my tag

and then I added the piece of CSS : .my-autocomplete .ac-container{ position : relative; top: 100% !important; left: 0 !important; width: 100% !important;

}

I tried to use the run on my main app but I get some error.

Many thanks for your quick response.

I will try more tomorrow :)

Best Regards
Abdou

@heese
Copy link

heese commented Oct 30, 2017

To override position_autocomplete the following code worked for me:

angular.module('app', ['ngSanitize', 'MassAutoComplete'])
.run(['massAutocompleteConfig', function(massAutocompleteConfig) { 
    massAutocompleteConfig.position_autocomplete(container, target) {
        // Your code.
    }
}]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests